What are the Storage Classes in C Programming Language?



What are the Storage Classes in C Programming Language?

A storage class tells us about the scope and lifetime of a variable or function with in a program. There are following Storage Classes in C:-

  • Automatic Storage Class
  • Static Storage Class
  • Register Storage Class
  • External Storage Class


Automatic Storage Class:-


The keyword for Automatic Storage Class is "auto". It is the default Storage Class for all local variables. If we declared a variable as auto then it is stored in the memory. The default value of this variable is garbage. The scope of this variable is local to block in which it is defined and it's lifetime will be remained till it's control will be with in the block. 


Example:-



  • #include<stdio.h>
  • void main()
  • {
  •           auto int var;
  •           printf(“%d”,var)
  • }



Output:-
  • 1045(Garbage Value)



Static Storage Class:-

The keyword for Static Storage Class is "static". It is the default Storage Class for global variables. If we declared a variable as static then it is stored in the memory. The default value of this variable is zero. The scope of this variable is local to block in which it is defined and it's lifetime will be remained till it's control will be with in the block. 

Example:-

  • #include<stdio.h>
  • static int var1;
  • int var2;
  • {
  •           printf(“%d”,var2)
  • }


Register Storage Class:-

The keyword for Register Storage Class is "register". It is used to define local variables and these local variables should be stored in register instead of RAM . If we declared a variable as register then it is stored in the CPU register. The default value of this variable is garbage. The scope of this variable is local to block in which it is defined and it's lifetime will be remained till it's control will be with in the block. 

Example:-

  • #include<stdio.h>
  • void main()
  • {
  •           register int var;
  •           printf(“%d”,var)
  • }

Output:-
  • 1045(Garbage Value)

External Storage Class:-

The keyword for External Storage Class is "extern". It is used to give reference to global variables that is visible to whole program files. If we declared a variable as extern then it is stored in the memory. The default value of this variable is zero. The scope of this variable is global(or local when it is defined with in block) and it's lifetime will be remained till as long as the program's execution does not finish.

Example:-

  • #include<stdio.h>
  • int var1;
  • void main()
  • {
  •           extern int var2;
  •           printf(“%d %d”,var1,var2)
  • }
  • int var2=20;

Output:-
  • 0,20


No comments:

Post a Comment