Storage classes in C

Ratings:
(4)
Views:0
Banner-Img
  • Share this blog:

About Storage classes in C Language

storage classes of c will provides following information to the compiler i,e

  1. storage area of a variable.
  2. scope of a variable i.e in which block the variable is visible.
  3. Lifetime of a variable i.e how long the variable will be there in active mode.
  4. Default value of a variable if it is not initialized

   

  • Depends on the behavior and storage area, storage classes are classified into two types i.e
    • Automatic storage class
    • static storage class
    •  
  1. Automatic storage class:

This storage class variables will created automatically and destroyed automatically.

  • Automatic storage class variables will stores in stack area of data segment or cpu register
  • Under automatic storage class, we having two type of storage class specifier i.e auto, register

 

  1. Static storage class:

  • Static storage class variables will created only once and throughout the program it will be there in active mode.
  • Static variables are stores in static area of data segment.
  • Under static storage class we having two types of storage class specifiers i.e static, exterm

  22  

Type Scope Life DEFAULT Value
auto body body Garbage value
static function program 0  
Extern program program 0  
         

      All functions  register body body garbage value.   In ‘c’ programming  language , there are 4 types of scopes are available. ie

  1. body scope
  2.  function scope
  3. program scope
  4. filescope.

  int a; short int a; signed short int a;   auto           signed        short          int            a;

↓        ↓      ↓       ↓    

Storage      sign            size             Type    variable.  

  • By default any variable storage class specifier is auto within the body.
  •  

Register variables :

It is a special kind of variables which stores in CPU registers.

  • The basic advantage of register variable are faster than remaining variables.
  • In implementation when we are using a variable through out the program “n” no.of times then recommended to go for register variables

     

Limitation :

  • Register memory is limited that why depends on the cpu capacity 4 to 6 are maximum variable can be constructed.
  • When we are working with the register variable pointer or pointer related, concept cannot be applied because it is not possible to access address of a register variable.

void main() { register int a=10; ++a; printf(“\na=%d”,a); printf(“\n Enter a value:”); scanf(“%d”,&a); ++a; printf(“\na=%d”,a); } // input value is 20 o/p : Error must take address of a memory location    

Void main()

{ Auto int a=5; ++a; Printf(“\n a=%d”,a); } Void main() { abc() abc(); abc(); } o/p : a=6 a=6; a=8;     23    

  • Void main()

{ auto int a=5; ++a; printf(“\n a=%d”,a); { int b=10; ++a; ++b; printf(“\n a=%d  b=%d”,a,b); } ++a; printf(“\n a=%d”,a);   } o/p: a=6; a=7;  a=8;b=11        

  • When sub body doesn’t containing any identifier which is declared already in main body, then automatically it will access in sub-body also
  • According K and Rc standard , the above program is error because all variable declarations must be placed top of the program after opening the main body only
  • According to ANSI – C standard the above program is valid because we can create the variable anywhere in the program after constructing the new body

 

  • void main()

{ int a=10; --a; printf(“\n a=%d”,a); auto int a=5; int b=15; ++a; --b; printf(“\n a=%d   b=%d”, a,b); } --a; ++b; printf(“\n a=%d   b=%d”,a,b); }   o/p: Error undefined symbol ‘b’   When we are creating a variable directly in subbody, then scope is restricted within the sub body only. That’s why, we can’t access the variable in main body.  

  • void abc()

{ int a=5; static int s=5; ++a; ++s; printf(“\n a=%d   s=%d”,a,s); }    

  • void main()

{ abc(); abc(); abc();   o/p: a=6     s=6 a=6   s=7 a=6   s=8   When we are working with the static variable, it will be created only once, when we are calling f’n for the first time and remaining all calls, same values are used     24     By default value of the static variable is 0 if we are not initialize the data, if it is auto variable by default value is garbage.    

  • void main()

{ static int s=10; --s; printf(“-n statics : %d”,s); { static int s=2-; --s; printf(“\n static 2: %d”,s); } }  

  • void main()

{ abc(); abc(); abc(); output : static1=9 static 2=9 static 1:8 static 2:18 static 1 : 7 static 2 :17        

void main()

{ static int s=10; ++s; printf(“\n s=%d”,s); } void main() { abc(); abc(); printf(“\n s=%d”,s); } o/p : Error undefined symbol ‘s’  

  • When we are working with static variable scope of the static variable is registered within the f’n, that why abc() function related data, we can’t access in main f’n.
  • When we are working with auto-variable scope and life time, both are restricted within the body. When we are working with static variable, scope is restricted within the f’n, but life time is not restricted
  • In implementation, when we are require no access a data in more than one f’n, then recommended to go for “Extern” variable i.e global variable are required
  • Whenever we are declaring a variable, outside of the f’n then it is called global variable,
  • The scope and lifetime of the global variable is not restricted.

    int g; // global variable void abc() { ++g; } void xyz() { ++g; 25   void main() { ++g; abc(); xyz(); prointf(“g=%d”,g); } o/p : g=3   Void abc() { ++g; } int g=5; void main() { ++g; abc(); printf(“g=%d”,g); }   o/p : Error undefined symbol ‘g’

  • Due to top to bottom, compilation process when we are compiling ‘++g’ statement in abc f’n , then it gives an error, because physical address is not available
  • When we are working with global variable, it is possible to create anywhere in the program and end of the program also.
  • In implementation when we are accessing a global variable, before creation to overcome the compilation erro, we require to gofor forward declaration by using extern keyword.

void abc() { ++g; // declaration } void main() { ++g; abc(); printf(“g=%d”,g); } o/p : g=7   The basic difference b/w declaration of a global variable and definition is :  

  • Declaration statement doesn’t occupies any physical memory, it avoids only compilation error.
  • When we are working with the definition of global variable, it will occupies physical memory of a variable.

  void abc() { ++g; } void xyz(); ++g; } void main() { ++g; abc(); xyz(); printf(“g=%d”,g); } o/p : Error undefined symbol ‘g’  

  • In order to access the global variable in abc, xyz & main f’n, we require to go for forward declaration of global variable. in abc, xyz and main also.
  • in implementation, when we require to provide forward declaration of a gloabal variable, more than once, then recommended to go for global declaration.
  • When we are declaring a global variable, top of the program before defining the first f’n, then it is called global declaration.
  • If the global declaration is available, then doesn’t require to go for local declaration, if local deceleration also available then global declaration will ignore.
  • extern int g: // global declaration

  void main() { // extern int g;              // local declaration ++g; } void xyz() { //extern int g; // local declaration ++g; }   void main() { // extern int g; // local declaration ++g; abc(); xyz(); printf(“g=%d”,g) ; } int g; o.p : g=3     void main() { ++g; } void main() { ++g; abc(); printf(“g=%d”,g); } o/p : Error undefined symbol = g(Liniking error)  

  • When we are working with the global variable, if declaration statement is not available, then we will get the error at the time of completion, if definition is not available, then we will get the error at the time of linking.

      Extern int g=5; void xyz(); { --g; } void main() { --g; xyz(); printf(“g%d”,g); }   int g o/p : - g=3;    

  • The initialization of the global variable can be placed in declaration or definition, not in both locations

extern int g=10;   void abc() { ++g; } void main() { ++g; abc(); printf("g=%d”,g); o/p: g=12  

  • When the declaration statement contains initialization, then physical memory will constructed in declaration statement so, doesn’t required to go for definition.
  • In above case, if definition also available then compiler will ignore definition statement

  extern int g=5; void abc() { ++g; } void main() { ++g; abc(0; printf(“g=%d”,g); } int g=10;   o/p : error variable ‘g’ is initialized more than once  

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox