Storage Classes in C Programming Language: Auto, Register, Extern & Static Storage Types
Posted on January 25, 2010
Storage Types in C Programming Language
A storage class is used to tell the compiler how to store the variables and functions within a program or code. It is generally represented as :
Storage-class type variable-name ;
Different types of storage classes used in a C program are :
- Auto
- Register
- Extern
- Static
The names of these storage classes comes under the list of keywords. Therefore these storage classes are known as reserved words or keywords.
Auto – Storage Class
The storage class auto refers to automatic variable.
Syntax:
Auto type variable-name;
That means the keyword auto just precedes the normal variable declaration.
Note :
In c language all the variables which are defined within a function are automatic or in other words we can say that if we do not apply keyword auto before a variable which is defined within a function will be treated as an automatic variable.
Auto is used only for the local variables.
Example of variable declaration with keyword auto is shown below :
Void main()
{
auto int age;
auto char name;
int roll_no;
int marks;
}
In the above example there are 4 variables defined inside the main function. Out of which two are defined using keyword auto and other two are defined without using it but all these variables will perform the same function.
Register – Storage Class
A register declaration or a register storage class has all the characteristics of an auto variable. But the difference is that, register variables provides faster access as these variables are stored inside the CPU registers instead of storing them in the memory. The register can be applied only to the local variables.
But variable stored in registers should not have any kind of operators attached to it as operators have no allocated memory in the registers.
Syntax:
register type variable-name;
Example of variable declaration with keyword register is shown below :
Void main()
{
register int distance;
register int count;
}
The variables with keywords are defined within the main function which shows that these variables are local variables.
Extern – Storage Class
C Language allows us to split the large program into different files. A file can contain some functions and rest of the files contains the other functions in the program. Then these files can be separately compiled and then later linked together for the program execution. If we have declared a global variable then in c we are not allowed to declare it again in some other file.
The ‘extern’ keyword comes into existence if that other file is using the global variable that is defined outside the file. The extern storage class tells the compiler that the variable types and names that follow it have been declared so that no fresh memory is allocated to these variables.
Note:
The extern storage class is used only for global variables.
- An extern variable is initialized by the program when the file is first loaded.
- Extern is a keyword which is used to represent external variable.
- The lifetime of the external variables remains as long as the program runs.
Syntax:
extern type variable-name;
extern function-prototype;
Example:
extern int I, j;
extern void cube (int x);
The keyword extern is optional for a function prototype.
Static – Storage Class
In C we can have static global variables as well as static local variables. The static variables are permanent within their own function in case of local variables and file in case of global variables. These variables maintain their values between calls. A static global variable is available only inside the file where it has been declared.
When a static modifier is applied to the local variable, it is initialized only when the very first call to the function occurs. It is not finished when the function terminates but it holds its value even after function’s termination but it can only be accessed within its own function.
Example:
Void fact (void)
{ static int num=10;
Int count=0;
}
A static global variable is useful when we want to create a global variable only for the function of one particular file.
Looking for a Fresher Job? Post Your Resume Free !!!
More Articles From "C Programming" Category
- Variable Types in C Programming Language
- Derived Data Types in C Programming Language
- C Language Operator Types: Operators in C
- C Programming Primary Constants: Integer, Real & Character
- Constants in C Programming Language: Primary & Secondary
- C Data Types: Range & Size
- Type Modifiers in C Language: signed, unsigned, long & short
- C Language Fundamental Data Types: Int, Float, Char, Double
- C Language Data Types
- C Language Keywords