C Programming : Storage classes introduction, examples and interview questions

C Programming ; Storage Classes in c

Storage Classes :

  • The storage class in c decide the scope, lifetime and visibility of a variable.
  • They precede the type that they modify. The specifiers which determine the type of storage classes in c are known as storage class specifiers.

There are four type of storage class specifiers in C :
  1. auto
  2. register
  3. static
  4. extern
 C Programming ; Storage Classes in c

Let's learn more about storage classes in c with the help of a diagram :

Storage specifier How it has declared Storage        Initial value    Scope         Life           
auto globally,locally stack garbage  within block end of block
extern globally,locally data segment zero global
multiple files
till end of program
static globally,locally data segment zero within block til end of program
register globally,locally CPU register garbage within block end of block

Now let's discuss each storage class separately.

➣Automatic storage class :

  • The auto storage class is the default storage class for all the local variables that means for all the variables which are defined inside a block or a function.

  • These variables are also called as local variables because they get destroyed after the execution of the block.

  • As the variables are created automatically, garbage values (could be anything) are assigned to them by the compiler.

  • The keyword auto is rarely used while writing programs in C.

Example of auto storage class :

C Programming ; Storage Classes in c; auto storage class

➣Register storage class :

  • The values stored in register storage class is same as the value stored in automatic storage class.

  • The values of register class get stored in CPU register instead of RAM for faster retrieval of values in program. Forex- counter lie values.

  • The default value of register variable is the garbage value.

  • It should also be noted that defining 'register' does not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register depending on hardware and implementation restrictions.

Example of register storage class :

C Programming ; Storage Classes in c; Register storage class

➣Static storage class :

  • The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope. 

  • The scope of the variable will not be effected i.e. variable remains local to the block in which it lies.
    Therefore, making local variables static allows them to maintain their values between function calls.
  • The static modifier may also be applied to global variables. When this is done, it causes that variable's scope to be restricted to the file in which it is declared.

C Programming ; Storage Classes in c; static storage class

➣External storage class :

  • The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern', the variable cannot be initialized however, it points the variable name at a storage location that has been previously defined.
  • The extern modifier is most commonly used when there are two or more files sharing the same global variables or functions as explained below.
  • Default value of any external variable is ‘0’ (zero).

C Programming ; Storage Classes in c; Extern storage class

   Interview questions on storage classes              

Q1. What are the different types of storage classes in c? Explain each one of them.
Each storage class can be asked by the interviewer separately.

Q2. Output of following program?
int main()
static int i=5;
printf("%d ",i);

Answer : 0000

Q3. #include
int main()
static int i=5;
if (--i){
printf("%d ",i);

Answer : 4321

Q4. Output?
int fun()
static int num = 16;
return num--;

int main()
for(fun(); fun(); fun())
printf("%d ", fun());
return 0;

Answer : 14 11 8 5 2

Previous                                                                                                                                        Next

For any query related to the content mail to devkumar082001@gmail.com

No comments:

Powered by Blogger.