Saturday, 24 August 2013

What are the different storage classes in C ?

A storage class defines the scope and life time of variables. 
There are four storage classes:

  • Auto Storage class
  • Register Storage class
  • Static Storage class
  • Extern Storage class


1.auto - Storage Class
auto storage class is the default for local variables.
    example
       {
            int a;
            auto int M;
}
The example above defines two variables with the same storage class. Auto Storage class  only be used within the functions, i.e. local variables.


2.register - Storage Class
register Storage Class is used to define local variables and to stored in a register instead of RAM. 
{
            register int  Miles;
}
Register must be used for variables that need quick access - such as counters.

3.static - Storage Class
static storage class is the default for global variables.

static int Count;
        int Road;

        {
            printf("%d\n", Road);
        }
static variables are 'visible' to all functions in this source file. 
The variable with static storage class will retain its value during various call.

   void func(void);
   
   static count=10; /* Global variable - static storage class*/
   
   main()
   {
     while (count--) 
     {
         func();
     }
   
   }
   
   void func( void )
   {
     static i = 5;
     i++;
     printf("i is %d and count is %d\n", i, count);
   }
   
   This will produce following result
   
   i is 6 and count is 9
   i is 7 and count is 8
   i is 8 and count is 7
   i is 9 and count is 6
   i is 10 and count is 5
   i is 11 and count is 4
   i is 12 and count is 3
   i is 13 and count is 2
   i is 14 and count is 1
   i is 15 and count is 0

4.extern - Storage Class
extern storage class is used to give a reference of a global variable.
'extern' variable cannot be initialized. 



Related Posts:

  • Storage Classes in c A storage class in C defines the scope (visibility) and life-time of a variables and/or a functions .These specifiers precede the type that they modify. There are some storage classes, which are  used in a C Pr… Read More
  • Operators in C An operator is a symbol that instruct  the compiler to perform specific mathematical or logical operation. C language  has following type of operators: C Programming •  Arithmetic Operators •  … Read More
  • Constant and Literals in c The constants refer to fixed values that the program may not change during its execution. These values are also called literals. C Programming Constants can be of any of the  data types like an integer … Read More
  • Loops in C  This is an attempt at explaining the loops in C .It is not complete, just the basics. C Programming Loop is  a construct which execute a set of statements certain number of times… Read More
  • Decision Making Structure In C Decision making structures allows the programmer to specify one or more conditions to be evaluated or tested by the compiler or , along with a statement or set of statements to be executed if the condition is  … Read More

0 comments:

Post a Comment