1. CPU Register
            2. Memory
By using storage class we would be find below cases,
            1. Where variable would be stored?
            2. Initial value of variable?(if not defined)
            3. Scope of the variable?
            4. Life time of the variable?
Types of storage classes in C,
- auto,
- static,
- register,
- extern.
Storage Class in One Table
| Storage Class(Memory) | Scope | Life Time | Declared | Can Be Referenced | Init With | Notes | 
|---|---|---|---|---|---|---|
| auto(Memory) | Within block | Within block | inside fn/b | inside fn/b | any expr | 1 | 
| static(Memory) | Within block | Value remain same Till program | outside fn | anywhere in file | constant expr | 2 | 
| inside fn/b | inside fn/b | constant expr | ||||
| register(CPU Reg) | Within block | Within block | inside fn/b | inside fn/b | any expr | 1,4,5 | 
| extern(Memory) | Throughtout Program | Program End | outside fn | anywhere in file | constant expr | 3 | 
| inside fun/b | inside fn/b | cannot be init | 
Notes:
1. Variable is init each time fn/b is entered; no default value.      2. Start of program execution value is zero(default);.
      3. Variable must be defined in only one place without extern.
4. Register assignment value not guaranteed; restricted (implementation dependent) types can be assigned to registers. & operator cannot be applied.
5. Defaults is auto.
6. If variable in global, Variable can be declared in only one place; start of program execution value is zero(default).
4. Register assignment value not guaranteed; restricted (implementation dependent) types can be assigned to registers. & operator cannot be applied.
5. Defaults is auto.
6. If variable in global, Variable can be declared in only one place; start of program execution value is zero(default).
*If not mention any storage class while variable declaration compiler takes default auto.
Example Programs
#include<stdio.h>
main()                                                                          
{
    int a = 10;                                                                        
    printf("%d",a);
}
#include<stdio.h>
main()
{
    auto int a = 10;
    printf("%d",a);
}
i). If not declared any storage class, default auto.
ii). Both are same program.( Output is 10 ).
#include<stdio.h>
int test( void )
{
    static int a = 1;
    printf("%d\n",i);
    i = i+1;  
}
main()
{
    test();
    test();
    test();
}
i.) Output is 
                    1
                    2
                    3
ii). Last value will be remain same in every entry of function.
#include<stdio.h>
main()
{
    register int a = 10;
    printf("%d",a);
}
i), We not sure a = 10; value is stored CPU register.
ii). CPU register are limited.
iii). If value is not stored in register, its storage class is auto
a.c                                                                                           
#include<stdio.h> 
int a = 10; //Global Variable
main()                                                                                     
{                                                                  
    test();
}
b.c
#include<stdio.h>
extern int a; //Extract variable from a.c
int test()
{
    printf("%d",a);
}
While using the extern we need to compile with both file like gcc a.c b.c -o sample.out.
 
 
  
0 Comments