Understanding Automatic Storage Class Complete Guide

Storage class:

Storage class is a keyword that represents the location and the scope of the variable.

Automatic storage class:

The automatic storage class is represented by the keyword ‘auto’. So, we can declare a variable as:

  auto int num;

When a variable is declared as ‘auto’. It is created on computer’s memory. it contains some garbage or junk values initially. It will be available only in the block and inner blocks where it is defined. Outside the block, it will not be accessible.

Program to understand the scope of the auto variable

#include<stdio.h>
void main()
{
   {   /* block 1 starts */
      auto int i=10;
   {   /* block 2 starts */
      printf("\n %d", i);
   }   /* block 2 ends */
      printf("\n%d",i);
   }   /* block 1 ends */
      printf("\n %d", i);
 }

In above program, we declared integer type variable i with the storage class ‘auto’ in block 1. Hence, that variable is available inside that block 1 and inside the block 2 also. But it is not available outside the block 1, since auto variable is removed from memory when coming out of that block.A block statement starts with a left curly brace and ends with right curly brace, as:

    {   /* block starts here */
        statements;
    }   /* block ends here */
Naveed Tawargeri
 

Hi, I'm Naveed Tawargeri, and I'm the owner and creator of this blog. I'm a Software Developer with a passion for Programming.