A storage class defines the scope (visibility) and life time, visibility, memory location, and initial value of variables and/or functions within a C Program. There are four types of storage classes in C
Storage Classes | Storage Place | Default Value | Scope | Lifetime |
---|---|---|---|---|
auto | RAM | Garbage Value | Local | Within function |
extern | RAM | Zero | Global | Till the end of the main program Maybe declared anywhere in the program |
static | RAM | Zero | Local | Till the end of the main program, Retains value between multiple functions call |
register | Register | Garbage Value | Local | Within the function |
The auto storage class is the default storage class for all local variables. The keyword used to specify a variable as as auto storage class is auto
. auto
can only be used within functions, i.e. local variables.
#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; }
#include <stdio.h> int main() { int a = 10, i; printf("%d ", ++a); { int a = 20; for (i = 0; i & lt; 3; i++) { printf("%d ", a); // 20 will be printed 3 times since it is the local value of a } } printf("%d ", a); // 11 will be printed since the scope of a = 20 is ended. }
The register storage class is used to define local variables that should be stored in a register instead of RAM(Depending upon the size of the memory remaining in the CPU). The keyword used to specify a variable as within register storage class is register
. So the variable has a maximum size equal to the register size (usually one word). The register should only be used for variables that require quick access such as counters.
#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); }
#include <stdio.h> int main() { register int a = 0; printf("%u", & a); // This will give a compile time error since we can not access the address of a register variable. }
The static storage class instructs the compiler to keep a local variable in existence during the lifetime of the program instead of creating and destroying it each time it comes into and goes out of scope. The keyword used to specify a variable as within static storage class is static
. The static
modifier may also be applied to global variables and it causes that variable's scope to be restricted to the file in which it is declared.
#include<stdio.h> static char c; static int i; static float f; static char s[100]; void main() { printf("%d %d %f %s", c, i, f); // the initial default value of c, i, and f will be printed. }
#include<stdio.h> void sum() { static int a = 10; static int b = 24; printf("%d %d \n", a, b); a++; b++; } void main() { int i; for (i = 0; i < 3; i++) { sum(); // The static variables holds their value between multiple function calls. } }
The extern storage class is used to give a reference of a global variable that is visible to all the program files. The keyword used to specify a variable as within extern storage class is extern
.
extern
is used to declare a global variable or function in another file. The extern
modifier is most commonly used when there are two or more files sharing the same global variables or functions.#include <stdio.h> int main() { extern int a; printf("%d", a); }
#include <stdio.h> int a; int main() { extern int a; // variable a is defined globally, the memory will not be allocated to a printf("%d", a); }
#include <stdio.h> int a; int main() { extern int a = 0; // this will show a compiler error since we can not use extern and initializer at same time printf("%d", a); }
#include <stdio.h> int main() { extern int a; // Compiler will search here for a variable a defined and initialized somewhere in the pogram or not. printf("%d", a); } int a = 20;
extern int a; int a = 10;# include < stdio.h > int main() { printf("%d", a); } int a = 20; // compiler will show an error at this line