Storage class specifiers (1)
Storage class specifiers (1)
SYNTAX:
storage_specifier data_type variable _name;
1. auto
2. extern
3. static
4. register
Storage
Specifier Description
Storage place: CPU Memory
Initial/default value: Garbage value
Scope: local
Auto Life: Within the function only.
Storage place: CPU memory
Initial/default value: Zero
Scope: Global
Life: Till the end of the main
program. Variable definition might be
Extern anywhere in the C program.
Storage place: CPU memory
Initial/default value: Zero
Scope: local
Life: Retains the value of the variable
Static between different function calls.
Storage place: Register memory
Initial/default value: Garbage value
Scope: local
Register Life: Within the function only.
● This is the default storage class for all the variables declared inside a
function or a block. Hence, the keyword auto is rarely used while writing
programs in C language.
● Auto variables can be only accessed within the block/function they
have been declared and not outside them (which defines their scope).
● These can be accessed within nested blocks within the parent
block/function in which the auto variable was declared.
● However, they can be accessed outside their scope as well using the
concept of pointers given here by pointing to the very exact memory
location where the variables resides.
● They are assigned a garbage value by default whenever they are
declared.
Example:
#include <stdio.h> }
int main( ) printf ( "\t %d ",j);
{ }
auto int j = 1; printf( "%d\n", j);
{ }
auto int j= 2; Output:
{ 321
auto int j = 3;
printf ( " %d ", j);
extern:
Example:
#include<stdio.h>
int x = 10 ;
int main( )
extern int y;
return 0;
int y=50;
Output:
The value of x is 10
The value of y is 50
static:
● This storage class is used to declare static variables which are popularly
used while writing programs in C language.
● Static variables have a property of preserving their value even after they
are out of their scope. Hence, static variables preserve the value of their
last use in their scope.
● They are initialized only once and exist till the termination of the
program. Thus, no new memory is allocated because they are not
re-declared.
● Their scope is local to the function to which they were defined. Global
static variables can be accessed anywhere in the program.
● By default, they are assigned the value 0 by the compiler.
● Static variables retain the value of the variable between different function
calls.
Example:
#include <stdio.h>
void next(void);
main() {
while(counter<10) {
next();
counter++; }
return 0;}
iteration ++;
Output:
register:
● This storage class declares register variables which have the same
functionality as that of the auto variables.
● The only difference is that the compiler tries to store these variables in the
register of the microprocessor if a free register is available.
● This makes the use of register variables to be much faster than that of the
variables stored in the memory during the runtime of the program.
● If a free register is not available, these are then stored in the memory
only. Usually few variables which are to be accessed very frequently in a
program are declared with the register keyword which improves the
running time of the program.
● The address of a register variable cannot be obtained using pointers.
Example:
#include <stdio.h>
int main()
register int i;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
for (i=0;i<5;i++)
return 0;
Output:
value of arr[0] is 10
value of arr[1] is 20
value of arr[2] is 30
value of arr[3] is 40
value of arr[4] is 50