Storage class in C
Storage class in C defines the scope, the visibility, and the lifetime of variables and functions. In other words, storage classes are used to describe the features of variables and functions. These features help the developers or programmers trace the existence of a particular variable during the program’s runtime.
They also precede the type that they modify. It also represents the location of variables and functions. It tells the compiler from which part of the code we can access the variable or function.
Types of Storage class in C
There are four types of storage class in C:
- Auto
- Extern
- Static
- Register
Storage specifier | Storage | Scope | Initial value | Life |
Auto | Stack | Within the block | Garbage | End of the block |
Extern | Data segment | Global | Zero | Until the end of a program |
Static | Data segment | Within the block | Zero | Until the end of a program |
Register | CPU Register | Within the block | Garbage | End of the block |
- Auto
- The variables that are used to define the auto storage class are also known as local variables.
- Auto means automatic storage class. A variable is present in the auto storage class by default if it is not specified explicitly.
- The scope of this variable is limited within the particular block. Once the control goes out of the block, the access will be destroyed; that is, the block in which the auto is declared can access it.
- By default, it consists of garbage value.
E.g.:
#include <stdio.h> #include <conio.h> #include <string.h> int main() { auto int j = 1; { auto int j = 2; { auto int j = 3; printf (“ The values are: %d \t”, j); } printf (“ %d \t”, j); } printf (“ %d \t”, j); }
Output
- Extern
- The extern class is used to give reference of a global variable, which is visible to all the program files.
- When extern is used, the variable cannot be initialized. However, it points the variable name at a storage location that has been defined previously.
- When there are multiple files, you can define a global variable or a function, which can be used in other files, then the extern will be used in another file to provide the reference of a defined function or variable.
- Extern is used to define a global variable or a function in other files.
- Extern is most commonly used when there are two or more files sharing the same global variable or function.
E.g.:
#include <stdio.h> #include <conio.h> #include <string.h> int a; int main() { extern int a; printf (“The value of a is: %d \n”, a); //default value is a return 0; }
Output
Note: In extern storage class, if a variable is declared as an external, then the compiler searches for that variable to be initialized somewhere in the program that may be static or extern. If it is not, then the compiler will throw an error.
- Static
- It is the type of storage class that is used to declare static variable which are used while writing programs in C.
- Static variables have a property of storing the value even after they are out of the scope.
- Hence, they preserve the value of their last use in their scope. It instructs the compiler to keep a local variable in existence during the life time of a program instead of creating and destroying it every time it comes in and goes out of scope.
- Later, the static makes local variables to maintain their values between function calls. No new memory will be allocated since they are not redeclared. Their scope is local to the function or variable to which they were defined.
E.g.:
#include <stdio.h> #include <conio.h> void function(void); static int count = 5; int main() { while (count--) { func(); } return 0; } void func(void) { static int i = 5; i++; printf (“i is %d and count is %d \n”, i, count); }
Output
- Register
- Register is used to declare register variables. They are supposed to be faster than the local variables.
- However, present data compilers are good at optimization of code and there will be rare chances of using the register variable that will make the program much faster.
- Register storage class is used to define local variables that are stored in register instead of the RAM, that is, the variable must have maximum size equal to that of register size and cannot have any unary operator applied to it.
E.g.:
#include <stdio.h> #include <conio.h> int main() { register int a; printf (“The value of a is: %d \n”, a); return 0; }
Output