Storage Classes in C
Categories: C language
Storage classes in C are used to determine the lifetime, visibility, memory location, and initial value of a variable. There are four types of storage classes in C
1. Automatic
2. External
3. Static
4. Register
Storage ClassesStorage PlaceDefault ValueScopeLifetime
autoRAMGarbage ValueLocalWithin function
externRAMZeroGlobalTill the end of the main program Maybe declared anywhere in the program
staticRAMZeroLocalTill the end of the main program, Retains value between multiple functions call
registerRegisterGarbage ValueLocalWithin the function
Automatic
1. Automatic variables are allocated memory automatically at runtime.
2. The visibility of the automatic variables is limited to the block in which they are defined.
3. The scope of the automatic variables is limited to the block in which they are defined.
4. The automatic variables are initialized to garbage by default.
5. The memory assigned to automatic variables gets freed upon exiting from the block.
6. The keyword used for defining automatic variables is auto.
7. Every local variable is automatic in C by default.
Example 1
#include <stdio.h>
int main()
{
int a; //auto
char b;
float c;
printf("%d %c %f",a,b,c); // printing initial default value of automatic variables a, b, and c.
return 0;
}
Output:
garbage garbage garbage
Static
1. The variables defined as static specifier can hold their value between the multiple function calls.
2. Static local variables are visible only to the function or the block in which they are defined.
3. A same static variable can be declared many times but can be assigned at only one time.
4. Default initial value of the static integral variable is 0 otherwise null.
5. The visibility of the static global variable is limited to the file in which it has declared.
6. The keyword used to define static variable is static.
Register
1. The variables defined as the register is allocated the memory into the CPU registers depending upon the size of the memory remaining in the CPU.
2. We can not dereference the register variables, i.e., we can not use &operator for the register variable.
3. The access time of the register variables is faster than the automatic variables.
4. The initial default value of the register local variables is 0.
5. The register keyword is used for the variable which should be stored in the CPU register. However, it is compiler?s choice whether or not; the variables can be stored in the register.
6. We can store pointers into the register, i.e., a register can store the address of a variable.
7. Static variables can not be stored into the register since we can not use more than one storage specifier for the same variable.
Example 1
#include <stdio.h>
int main()
{
register int a; // variable a is allocated memory in the CPU register. The initial default value of a is 0.
printf("%d",a);
}
Output:
0
External
1. The external storage class is used to tell the compiler that the variable defined as extern is declared with an external linkage elsewhere in the program.
2. The variables declared as extern are not allocated any memory. It is only declaration and intended to specify that the variable is declared elsewhere in the program.
3. The default initial value of external integral type is 0 otherwise null.
4. We can only initialize the extern variable globally, i.e., we can not initialize the external variable within any block or method.
5. An external variable can be declared many times but can be initialized at only once.
6. If a variable is declared as external then the compiler searches for that variable to be initialized somewhere in the program which may be extern or static. If it is not, then the compiler will show an error.
Example 1
#include <stdio.h>
int main()
{
extern int a;
printf("%d",a);
}
Output
main.c:(.text+0x6): undefined reference to `a'
collect2: error: ld returned 1 exit status