In C\C++ language, each variable has a storage class which decides the following things:
- Default initial value i.e if we do not explicitly initialize that variable, what will be its default initial value.
- Lifetime of that variable i.e for how long will that variable exist.
- Determines the part of memory where storage is allocated for an object and how long the storage allocation continues to exist.
- Determines the scope which specifies the part of the program over which a variable name is visible, i.e. the variable is accessible by name.
The are four storage classes in C
- auto
Variables which are defined within a function or a block ( block is a section of code which is grouped together. eg.statements written within curly braces constitute a block of code ) by default belong to the auto storage class. These variables are also called local variables because these are local to the function and are by default assigned some garbage value. Since these variables are declared inside a function, therefore these can only be accessed inside that function. There is no need to put ‘auto’ while declaring these variables because these are by default auto.Though you have been using these auto(local) variables in your entire C tutorial, let’s see one example.#include <stdio.h> int sum(int n1, int n2){ auto int s; //declaration of auto(local) variable s = n1+n2; return s; } int main(){ int i = 2, j = 3, k; k = sum(i, j); printf("sum is : %d\n", k); return 0; }
- extern
This keyword before a variable to tell the compiler that this variable is declared somewhere else. Basically, by writingextern
keyword before any variable tells us that this variable is a global variable declared in some other program file. A global variable is a variable which is declared outside of all the functions. It can be accessed throughout the program and we can change its value anytime within any function. We can do so by using extern keyword as shown below.// Global variable declared in file firstfile.c int g = 0; // 'g' declared as extern in a header file firstfile.h extern int g; // Include the header file "firstfile.h" in file secondfile.c in order to use the global variable 'g' #include "firstfile.h" main(){ g = 4; printf("%d", g); }
- static
A variable declared as static once initialized, exists till the end of the program. If a static variable is declared inside a function, it remains into existence till the end of the program and not get destroyed as the function exists (as in auto). If a static variable is declared outside all the functions in a program, it can be used only in the program in which it is declared and is not visible to other program files(as in extern). Let’s see an example of a static variable.#include <stdio.h> static int g = 5; void fn(){ static int i = 0; printf("g = %d\t", g--); printf("i = %d\n",i++); } int main(){ while(g >= 2) fn(); return 0; }
- register
It tells the compiler that the variable will get stored in a register instead of memory (RAM). We can access a register variable faster than a normal variable. Not all the registers defined as register will get stored in a register since it depends on various restrictions of implementation and hardware. We cannot access the address of such variables since these do not have a memory location as which becomes clear by the following example.#include <stdio.h> int main() { register int n = 20; int *ptr; ptr = &n; printf("address of n : %u", ptr); return 0; }
volatile keyword
volatile
keyword tells the compiler not to optimize anything that has to do with the volatile variable. It actually came into existence for the purpose of not caching the values of the variable automatically. It will tell the machine not to cache the value of this variable. So it will take the value of the given volatile variable from the main memory every time it encounters it. This mechanism is used because at any time the value can be modified by the OS or any interrupt. So using volatilewill help us accessing the value afresh every time.
// If variable is declared volatile, the compiler is forced to load it every time, // because it can be modified elsewhere. op= 0; while (!op) { /* Loop visible to the compiler */ }