C Storage Class Specifiers – auto, register & extern
C storage class specifiers determines the physical location in memory within the computer where the variable declared in c would be actually stored.
Storage class specifiers also determines
- The initial value of the variable, if an initial value is not assigned.
- The life of a variable.
- The scope of the variable.
Available c storage class specifiers in the c programming language are as follows:
auto Storage Class Specifier
The variables declared in c using auto storage class are initialized with an unpredictable initial value often called as garbage value and can be accessed from anywhere within the block in which the variable is declared. As soon as the program control exits from the block in which it is defined, the variable is destroyed.
By default variable in c use the auto storage class.
Syntax
auto data_type variable_name;
For example,
#include<stdio.h> main() { auto int i=1; { { { printf("n%d",i); } printf("n%d",i); } printf("n%d",i); } return 0; }
register Storage Class Specifier
The variables declared using register storage class specifier are treated similarly like that defined by auto storage class specifier with the only difference is that the variables are stored within the CPU registers providing faster access.
It is recommended to use register storage class for variables which are being used at many places.
Syntax
register data_type variable_name;
For example,
#include<stdio.h> main() { register int i; for(i=1; i<=100; i++) printf("n%d",i); return 0; }
static Storage Class Specifier
The variables declared with static storage class specifier are initialized with zero initial value if any initial value is not provided at the time of declaration and it can be accessed from anywhere within the block in which it is defined.
However, the variables declared with static storage class are not destroyed even after program control exits from the block. Thus, the value of the variable persists between different function calls.
Syntax
static data_type variable_name;
For example,
#include<stdio.h> increment() { static int i=1; printf("%dn",i); i++; } main() { increment(); increment(); increment(); return 0; }
Output
1
2
3
Extern Storage Class Specifier
The variable declared using extern storage class are stored in memory with by default zero initial value and continue to stay within the memory until the program’s execution is not terminated.
Moreover, variables declared as extern can be accessed by all functions in the program, thus avoiding unnecessary passing of these variables as arguments during function call.
It should be noted that the variables declared outside any function definition are treated as variables with extern storage class.
Syntax
extern data_type variable_name;
For example,
#include<stdio.h> int i; main() { printf("ni=%d",i); increment(); increment(); decrement(); decrement(); } increment() { i++; printf("nOn increment, i=%d",i); } decrement() { i--; printf("nOn decrement, i=%d",i); }
Output
i=0
On increment, i=1
On increment, i=2
On decrement, i=1
On decrement, i=0
4 Comments
at ·
I think this is a real great blog article.Much thanks again. Will read on…
at ·
Thanks A Million!!
at ·
thanx a lot,…..
at ·
wow its really great site i good n clear examples n last but not the least thanks a lot…………………:-)