-->

18. C-Storage Classes

C-Union: Previous                                                                          Next: C-PreProcessor
Storage classes refer to ‘availability’ of a variable. ‘Availability’ can be defined as:
  • Life time of variable
  • Scope of variable
  • Visibility of variable
Scope of variable, it describes those areas of program, where a variable can be accessed. Scope also affects a variable`s lifetime.
Life time of variable, it defines how long the variable persists in memory, or we can say that when space is allocated or de-allocated to variable.

Types of Scopes: - 

Generally there are 4 types of scope where a variable can be visible to programmer.
  • Program scope
  • File scope
  • Function scope
  • Block scope

Program scope: - This scope defines that a variable is available in entire program. We can say that a variable is active among different source files.

File scope: - It signifies that variable is available from the point where it is declared i.e. after the main( ) function till the end of file.

Function scope: - It signifies that a variable is active with in a function block only.

Block scope: - it signifies that the variable is available from its declaration point till the end of block.

Local and Global Variable: - The variables are generally classified into two:

  • Local variable
  • Global variable
Local variable: - ‘local’ is the scope of variable. Local variables are private. They are private to the block in which they are declared.
For Eg: -  function addition(int a, int b)
                        {
                                        int sum = 0;             /* Local variable */
                                        sum = a + b;
                                        return sum;
}


The variable sum is only available within the function. The same variable name can take for other variable within a program but in same block. Like in other function or in main( ). The variables are by default initialized with garbage value in c programming.

Global variable: - Their scope is within the program these variables are defined outside the main( ).
The priority of Global variables are high.
        For Eg: -  int a, b;    /* Global Variable */
                        void main( )
                        {
                                        a = 20;     /* Global variable accessible */
                                        sum( );
                        }
                        sum( )
                        {
                                        int ans;
                                        ans  = a + b;
                        }
There are 4 types of storage class specifiers :
  1. Auto
  2. Static
  3. Register
  4. Extern
  1. auto: -

Life time:                                 Only to the block in which they are declared.
Visibility:                                 We can read/write only in that block in which they are declared. Like in function or in main( ).
Location:                                    They occupy the space in primary memory i.e. RAM.
Initialization:                                Since they are local variables so they by default initialized with                                                                 garbage value.
‘auto’ keyword is applied with the variable name.
  1. static: - 

Life time:                                 Remains in the memory till the End of the program.
Visibility:                                 We can read/write only in that block in which they are declared.
Location:                                   They occupy the space in primary memory i.e. RAM.
Initialization:                              They are initialized with Zero.
‘static’ keyword is applied with the variable name.
  1. register: - 

Life time:                                 Only to the block in which they are declared.
Visibility:                                 We can read/write only in that block in which they are declared.
Location:                                     They occupy the space in Registers of CPU.
Initialization:                                 They are by default initialized with garbage value.
‘register’ keyword is applied with the variable name.
  1. extern: - 

Life time:                                 Remains in the memory till the End of the program.
Visibility:                                 We can read/write anywhere in entire program.
Location:                                     They occupy the space in primary memory i.e. RAM.
Initialization:                                 They are by default initialized with Zero.

‘extern’ keyword is applied with the variable name.
C-Union: Previous                                                                          Next: C-PreProcessor