Skip to main content

Storage classes in C

In C variables are declared by the type of data they can hold. During the execution of the program, these variables may be stored in the registers of the CPU or in the primary memory of the computer. To indicate where the variables would be stored, how long they would exist, what would be their region of existence, and what would be the default values, C provides four storage class specifiers that can be used along with the data type specifiers in the declaration statement of a variable. These four storage class specifiers are

The storage class specifier precedes the declaration statement for a variable.
auto int x;
extern int y;
register int z;
static int s;
We will discuss these storage class one by one
1.Automatic storage class
By default, all variables declared within the body of any function are automatic. The keyword auto is used in the declaration of a variable to explicitly specify its storage class. Even if the variable declaration statement in the function body does not include the keyword auto, such declared variables are implicitly specified as belonging to the automatic storage class.
These variables are created automatically, when the function in which it is declared is evoked. These variables are created in primary memory and behaves as local variables of the function. Their region of use is limited within the function body and by default they are not initialized.
The following program explains the use of automatic and global variables and their scope.
#include <stdio.h>
int i=10;
void f1()
auto int i=20;
printf(“The value of i in function f1 is =%d”,i);
void f2()
printf(“The value of i in function f2 is=%d”,i);

int main()
f1(); /* prints 20 ..uses the local variable*/
f2(); /prints 10 ..uses the global variable*/
2.External Storage class

A program in C, particularly when it is large, can be broken down into smaller programs. After compiling these, each program file can be joined together to form the large program. These small program modules that combine together may need some variables that are used by all of them. This provision can be made by specifying these variables as external storage class variable. These variables are global to all the small program modules that are formed as separate files.
The keyword for declaring such global variable is extern. Such global variables are declared like any other variable in one of the program modules while the declaration of these variables are preceded with the keyword extern in all other combining program modules. The program modules may also be a function or a block. These variables remain in existence as long as the program in execution. These variables are stored in primary memory and their default value is zero.
/**** program file prgm1.c ****/
#include <stdio.h>
#include “prgm2.c”
int i;
void show(void);
int main()
show(); /* show() function in another program file */
printf(“The value of  i in prgm1.c=%d”,i);
/***** program file prgm2.c *****/
extern int i;
void show()
printf(“The value of i in progm2.c=%d”,i);
The value of  i in progm2.c=20
The value of  i in progm1.c=20

3.The Register Storage Class
The variables stored in register of the CPU are accessed in much lesser time than those stored in the primary memory. To allow the faster access time for variable, the register storage class specifier is used. In most C compilers register specifier can only be applied to int and char.ANSI C has broadened its scope.The default value of these variable is garbage and their use is restricted within the region of a function or a block where it has been declared.
#include <stdio.h>
int power(int,register int);
int main()
int x=4;
int k=3;
int p;
printf(“The value of %d power %d is=%d”,x,k,p);
int power(int x,register int k)
register int temp;
for(;k;k- -) temp=temp*x;
return temp;

The value of 4 power 3 is 64

4.The Static Storage Class
The use of static storage class specifier makes a variable a permanent variable within a specified region. Two kinds of variables are allowed to be specified as static variables: local variables and global variables. The local variables are also referred to as internal static variables while the global variables are also known as external static variables. The default value of the static variable is zero.
A static local variable is allotted a permanent storage location in the primary memory. This variable is usable within functions or blocks where it is declared and preserves its previous value held by it between function calls or between block re-entries. However once a function is ivoked, the static local variable retains the value in it and exists as long as the main program in execution.
The external static variables in a program file are declared like global variables with the keyword static preceding its declaration statement. These variables can be accessed by all functions except those functions defined earlier in the same file or not accessible to functions defined in other files although these may use the extern keyword. These variables exist throughout the period of the main program in execution.
#include <stdio.h>
void show(void)
static int i;
printf(“The value of static variable i=%d”,i);
int main()
printf(“First call of show function\n”);
printf(“Second call of show function\n”);
printf(“Third call of show function\n”);

First call of show function
The value of static variable i=0
Second  call of show function
The value of static variable i=1
Third  call of show function
The value of static variable i=2


Popular posts from this blog

KTU C programming question paper and evaluation scheme

PART A 1 An identifier is a sequence of characters invented by the programmer or to identify or name a specific object. The sequence of characters may be letters, digits, and special character ‘_’known as an underscore Rules: i)Identifiers should start with alphabets. ii)Identifiers are case sensitive iii)A numeric digit should not be the first character iv)Identifier name should not be a keyword v)Identifier may be of any reasonable length 1mark

2mark 2 Associativity defines the direction, left to right or right to left in which operator act upon its operands Unary operators have associativity is from right to left. For examplein the expression &--x, pre decrement works first and then address of operator works Direction + example<

Files in C

A file is a repository of data that is stored in a permanent storage media, mainly in secondary memory. In order to use the files we should learn how to read information from a file and how to write information into a file. A very important concept in C is the stream.The stream is a common logical interface to the various devices( files).A stream is linked to a file while using an open operation. A stream is disassociated from a file while using a close operation. The current location, also referred to as the current position, is the location in a file where the next fie access will occur.There are two types of streams text and binary. The following are some difference between text and binary files ·Text file is human readable because everything is stored in terms of text. In binary file everything is written in terms of 0 and 1, therefore binary file is not human readable. ·A newline(\n) character is converted into the carriage return-linefeed combination before being written to the d…

Linear and Binary Search in C

Searching is one of the most common problems that arise in computing. Searching is the algorithmic process of finding a particular item in a collection of items. A search typically answers either True or False as to whether the item is present. On occasion it may be modified to return where the item is found. Search operations are usually carried out on a key field. Consider searching for a given value k in an array A of size n. There are 2 basic approaches: sequential search and binary search.
Linear (Sequential) Search When data items are stored in a collection such as a list or array , we say that they have a linear or sequential relationship. Each data item is stored in a position relative to the others. In C array, these relative positions are the index values of the individual items. Since these index values are ordered, it is possible for us to visit them in sequence. This process gives rise to our first searching technique, the sequential search. Starting at the first item in …