What is Storage Classes?
To define a variable in C language, its data type needs to be defined in the syntax ‘data type=identifier;’. For example, ‘int=a;’ defines the variable ‘a’ as an integer type of data. The data type represents the type of data to be assigned to the variable and the memory space to be allocated to it. To actually store the variable in the memory, a storage class needs to be mentioned along with the data type in the syntax ‘storage class data type=identifier’. For example, ‘auto int=a;’ defines variable ‘a’ as an integer type data in an automatic storage class. The storage class defines the parameters of the variable including where it is stored and accessed in the memory, the default value type, its scope, and its lifetime.
One of the parameters defined by storage class is the scope of the variable, or how long the variable shall be stored during the execution of the program. A variable may have the following scope:
1) Block Scope:
Sample Program:
void main()
{
{ //Block Close//
int a=10; //Block Scope, local variable, value 10//
printf(“a: %d ”, a); //Output: 10//
} //Block Close//
printf(“a: %d ”, a); //Output: Error//
}
The first output is 10 because the variable ‘a’ has been defined within the first block as having a value of 10. The second output shows an error because the variable ‘a’ has been defined inside the block and cannot be accessed from outside.
2) Method or Function Scope:
Sample Program:
void main()
{ //main() Method Open//
int a; //Method or Function Scope, local variable, garbage value//
{ //Block Open//
int a=10; //Block Scope, local variable, value 10//
printf(“%d”, a); //Output: 10//
} //Block Close//
printf(“%d”, a); //Output: garbage value//
check();
} //main() Method Close//
void check()
{ //check() Method Open//
printf(“%d”, a); //Output: Error//
} // check() Method Close//
The first output is 10 as the variable ‘a’ is defined inside the first block as 10. The second output is a garbage value as the variable ‘a’ was defined before the first block without a fixed value, that is, garbage value. The third output shows an error because the variable ‘a’ was defined within function main(), and not within the function check(), hence it could not be accessed outside function main().
3) Program Scope:
Sample Program:
int a=10; //Program scope, global variable, value 10//
void main()
{ //main() Method Open//
int a=20; //Function scope, local variable, value 20//
{
int a; //Block scope, local variable, garbage value//
printf(“%d”, a);//Output: garbage value//
}
{
a=a+10;//Operation done: a=20+10=30//
}
printf(“%d”, a);//Output: 30//
check();
} //main() Method Close//
void check()
{ //check() Method Open//
printf(“%d”, a); //Output: 10//
} //check() Method Close//
The first output is garbage value as the variable ‘a’ is defined in the block without a specified value, so it is returned as its default garbage value. The second output is 30 as the variable ‘a’ has been defined with value 20 locally in the previous block, and value is added by 10 with the operator ‘a=a+10’. The third output is 10 as the second method check() accesses the value of ‘a’ from the globally defined value of ‘a’ as 10.
Types of Storage Classes:
There are 4 types of storage classes based on the memory location of the variable, default value of the variable, scope of the variable, and lifetime of the variable.
- Automatic Storage Class
- Register Storage Class
- Static Storage Class
- External Storage Class
Automatic Storage Classes:
If storage class is not defined while declaring a variable, it is defined under automatic storage class by default. Automatic storage classes house only local variables and global variables cannot be declared.
- Keyword: auto (if no keyword is mentioned, the variables will be defined under automatic class by default)
- Memory Location: RAM
- Default Value: Garbage value
- Scope: Declaration inside block or method. Variables in automatic storage classes can be accessed only within the block or method in which it is defined.
- Lifetime: Till the block or the method is executed
Sample Program:
void main()
{
auto int a=10; //Method or Function scope//
{
auto int a;
printf(“a: %d ”, a); //Block scope, output: a=garbage value//
}
printf(“a: %d ”, a); //Output: a=10//
}
If the variable is defined globally (before ‘void main()’ in the program), the program will show error while execution.
Register Storage Classes
Like automatic classes, these classes also define local variables only, and global variables cannot be declared. Variables should be used in register class only when they need to be used repeatedly like loops.
- Keyword: register
- Memory Location: CPU register
- Default Value: Garbage value
- Scope: Declaration inside block or method. Variables in automatic storage classes can be accessed only within the block or method in which it is defined.
- Lifetime: Till the block or the method is executed
Sample Program:
void main()
{
register int n, sum=0, I;
printf(“Enter n:”);
scanf(“%d”, &n);
for(i=1; i<=n; i++);
{
sum=sum+i;
}
printf(“sum: %d”, sum);
}
Access to the variables in register classes is much faster than those in automatic classes as they are stored directly in the CPU. The computer has three separate memory parts working together, CPU register, primary memory, and secondary memory. The program entered is stored in the secondary memory (hard disk). While running, it is imported into the primary memory (RAM), where variables in an automatic class are defined. The execution of the program is itself performed by the CPU, which picks up instructions line by line from the RAM while executing them. In the above program, the variables to be defined are sum (value 0), n (value to be entered by user), and I (value 1 defined in the for loop). If these values are defined under automatic class, the program will repeatedly refer to the RAM as long as the loop is running from the CPU since the variables are defined in the RAM. However, if the variables are defined under register class, the values will be defined directly in the CPU and the program does not need to run back and forth between the CPU and RAM and will execute much faster.
However, all variables cannot be defined in the register class to save the time of execution, as the CPU register memory is very small and is used to execute the program. If all variables are stored in register class, the CPU cannot allocate enough free space in its register for the program to run. Hence most values are defined in RAM in automatic class and only repeatedly used variables (used in loops) are defined in register class.