Storage classes are used to know the scope and lifetime of a variable. There are 2 types of storage classes.
- Local
- Global
Below are different storage class specifiers:
- auto
- register
- static
- extern
Before looking into storage classes, we shall discuss about variables and how they behave in different blocks. A block in C, are set of statements those are written inside “{ … }” braces.
Below are different types of blocks available in C:
- Function block.
- File Block.
- Global Block.
- Nested block.
1. Automatic variables:
Any variables declared inside a function will be of automatic storage class. “auto” is the keyword used to declare a variable as a automatic storage class.
- Automatic variables are stores in the stack segment.
- Their scope is with in the block they are defined.
- Their lifetime is till the execution of the function.
- Their default value will be garbage if not defined.
Example:
auto int i = 10; int j = 20;
Example:
#include<stdio.h> int main(int argc, char const *argv[]) { int num = 10; { int num = 20; printf("The num value inside nested block = %d \n", num ); } printf("The num value inside main block = %d \n", num ); return 0; }
Output:
The num value inside nested block = 20 The num value inside main block = 10
2. Register Specifier:
“register” is the keyword used to declare a variable as a register storage class. A variable declared as a register, is stored in the computer register memory. However, this is just a request to the compiler to store the value in a register and is not guaranteed that it will be stored there.
- Register variables are stores in the computer register if available.
- Their scope is with in the block they are defined.
- Their lifetime is till the execution of the block.
- Their default value will be garbage if not defined.
Example:
register int i = 10;
3. Static Specifier:
“static” is the keyword used to declare a variable as a static storage class.
- static variables are stores in the data segment.
- Their lifetime is till the execution of the program.
- Their default value will be zero if not defined.
- If a local variable is declared as static, then that variable will be maintaining its value between the function calls.
- If a global variable is declared as static, then that variable scope is that of the file. That variable cannot be accessed from other files.
Example:
static int i = 10;
Program example:
#include<stdio.h> static int num; void func_1() { static int called= 1; printf("This function is called %d times\n",called ); called++; } int main(int argc, char const *argv[]) { printf("The global value of static variable is %d\n", num); func_1(); func_1(); func_1(); func_1(); return 0; }
Output:
The global value of static variable is 0 This function is called 1 times This function is called 2 times This function is called 3 times This function is called 4 times
4. Extern Specifier:
“extern” is the keyword used to declare a variable as an extern. It is an indication for the programmer that the variable is defined in other place, and it is using a reference. Initialization of the variable cannot be done, because it has been defined and initialized in other file.
- extern variables are stores in the data segment.
- Their lifetime is till the execution of the program.
- Their default value will be zero if not defined.
- The scope is in the file the variable has been imported.
Example:
extern int i = 10;
Programming example:
1.c
#include<stdio.h> extern void func_1(); int num; int main(int argc, char const *argv[]) { printf("In the main function, global value of variable is %d\n", num); func_1(); return 0; }
2.c
#include<stdio.h> extern int num; int func_1() { printf("In the file 2, in finc_1 function, value of extern variable is %d\n", num); // initialization is wrong, but you can change the value of that variable. num += 20; printf("In the file 2, in finc_1 function, value of extern variable after changing value is %d\n", num); return 0; }
Compiling both of the programs: gcc 1.c 2.c Output: ./a.out
In the main function, global value of variable is 0 In the file 2, in finc_1 function, value of extern variable is 0 In the file 2, in finc_1 function, value of extern variable after changing value is 20
List Of Tutorials available in this website: