Showing posts with label Storage class specifier in c. Show all posts
Showing posts with label Storage class specifier in c. Show all posts

Saturday, January 4, 2014

Storage Class Specifier In C

1. What is storage class specifier?

Storage class specifiers in C language tells the compiler where to store a variable, how to store the variable, what is the initial value of the variable and life time of the variable. 

Syntax: storage_specifier data_type variable _name

Types of storage class specifier in c :

 There are four storage class specifier in C Language. They are,
1. Automatic
2. Register
3. Static
4. Extern

S.No.
Storage Specifier
Storage place
Initial / default value
Scope
Life
1
auto
CPU Memory
Garbage value
local
Within the function only.
2
extern
CPU memory
Zero
Global
Till the end of the main program. Variable definition might be anywhere in the C program
3
static
CPU memory
Zero
local
Retains the value of the variable between different function calls.
4
register
Register memory
Garbage value
local
Within the function

 Note:

    • For faster access of a variable, it is better to go for register specifiers rather than auto specifiers.
    • Because, register variables are stored in register memory whereas auto variables are stored in main CPU memory.
    • Only few variables can be stored in register memory. So, we can use variables as register that are used very often in a C program.

Example Program for Auto variable in C

#include
void increment(void);

int main()
{
   increment();
   increment();
   increment();
   increment();
   return 0;
}

void increment(void)
{
   auto int i = 0 ;
   printf ( "%d ", i ) ;
   i++;
}


 Output: 0 0 0


Example program for static variable in C
 
#iclude
void increment(void);

int main()
{
   increment();
   increment();
   increment();
   increment();
   return 0;
}

void increment(void)
{
   static int i = 0 ;
   printf ( "%d ", i ) ;
   i++;
}


Output: 0 1 2 3

 
Example program for extern variable in C


The scope of this extern variable is throughout the main program. It is equivalent to global variable. Definition for extern variable might be anywhere in the C program.

#inlude
int x = 10 ;
int main( )
{
   extern int y ;
   printf ( "The value of x is %d \n", x ) ;
   printf ( "The value of y is %d",y ) ;
   return 0;
}
int y = 50 ;


Output:  The value of x is 10
                 The value of y is 50



Example program for register variable in C:

  • Register variables are also local variables, but stored in register memory. Whereas, auto variables are stored in main CPU memory. 
  • Register variables will be accessed very faster than the normal variables since they are stored in register memory rather than main memory.But, only limited variables can be used as register since register size is very low. (16 bits, 32 bits or 64 bits)


#include

int main()
{
   register int i;
   int arr[5];          // declaring array
   arr[0] = 10;         // Initializing array
   arr[1] = 20;
   arr[2] = 30;
   arr[3] = 40;
   arr[4] = 50;
   for (i=0; i < 5 ; i++)  {
      // Accessing each variable
      printf("value of arr[%d] is %d \n", i, arr[i]);
   }
   return 0;
}


Output: 

value of arr[0] is 10
value of arr[1] is 20
value of arr[2] is 30
value of arr[3] is 40
value of arr[4] is 50