Topics

6/recent/ticker-posts

Types of Storage Classes in C language (part 1)

 Types of Storage classes 

Generally the storage classes are majorly classified into 
1.  automatic storage class

2.  register storage class

3.  static storage class.
4.  external storage class.


Automatic storage class:

It is a default storage class. Hence auto key word is an optional.

 

Declaration:

  auto  int a ;  ( or ) int  a;

example to check default values

#inlclude<stdio.h>

int main()

{

            int  a;

            float  b;

            char ch;

            printf(“\n %d  %f  %c”,a,b,ch);

            return 0;

}

 

O/P will be all are garbage values.

 

Program to check scope of the variable

#include<stdio.h>

int main()

{

   int a=10;

   printf("\n %d ",a);

     {

            int a=20;

            printf("\n %d",a);

            {

              int a=30;

              printf("\n %d",a);

            }

            printf("\n %d",a);

     }

     printf("\n %d",a);

return 0;

}


     The above program is a general program and the below program is for checking scope or life of a program 


#include<stdio.h>

int main()

{

     {

 

            {

              int a=30;à scope of this variable “a” is within the inner block only

              printf("\n %d",a);

            }

            printf("\n %d",a);à error, variable a is not declared .

     }

     printf("\n %d",a);  à error, variable a is not declared .

return 0;

}




Program to check life

 

#include<stdio.h>

main()

{

            f1();

            f1();

            f1();

}

 

f1()

{

   int  a=10;

   printf(“\n  %d “, a);

   a = a + 10 ;

}    

            


    Auto storage class variable life is within the block.

    In above program, variable ‘a’ acquires memory when control entered into f1() and clears from memory once control come out of f1().




Register storage class:

 

Syntax :

             register int a;

 

1. Register is a 16 bit memory space.

2. Register are at CPU level.

3. Registers can be used by all programs running in the ram memory.

4. Registers can’t  be shared.

5. For 32 machine size of register is 32bit

6. If size of the variable is more than capacity of the register, then system ignores the register storage class and treats the variable as auto storage class.

7. CPU has only 16 registers if all registers all filled, next register variable fails and treated as auto storage type.

 

Advantage of register :


Variables can be accessed faster

Register are used in scenario where the  variables are accessing frequently. Register and auto programming same only difference is storage location.

 

 

Note: The programs given here are pre compiled and runned. And the user is free to use the compiler button provided after each and every program.





Post a Comment

0 Comments