Storage Classes in C

05 min read

Storage classes refer to the permanence of a variable (ie., lifetime) and its scope within the program. Scope refers to the portion of the program in which the variable is recognized. There are 4 different storage-class specifications in C:

  1. automatic
  2. external
  3. static
  4. register

1) Automatic variables: Variables which are declared within a function and which are local to that function are called automatic variables. Outside that function, these variables cannot be accessed. The keyword 'auto' can be placed before the variable name but this is optional.

int main()                                         
{
int n;                                                            

(or)

int main()
{
auto int n;

 

Both of these refer to automatic variables. So,  only within this main function, these variables are accessible.

Example:

int main()
{
int 11=10, x;
function1(n);
}


int function1(int n)
{
printf("%d",n);
printf("%d",x);
return n;
}

This program will show error, because the variable 'x' is not accessible in the user-defined function. The scope of 'x' is only within the main().

 

2) External variable (or Global variable): These are the variables declared outside of any function and can be accessed by any function foIlowing the declaration.

Example:

int n=10;
int main()
{
printf("%d",n);         ->  Output: 10
n=n+10;
function1();
printf("%d",n);         ->  Output: 30
}

int function1()
{
printf("%d",n);           -> Output: 20
n=n+ I 0;
return 1;
}

If the external variable 'n', should be used across 2 files, then the variable 'n' will be redefined in the second file with the keyword 'extern'. If a local variable inside a function is declared with the same name as that of the global variable, then inside that function, local variable takes precedence over global variable.

Example:

int m=5;
int function1()
{
int m=10;
printf("%d",m);        -->  Output: 10. Here local variable 'm' takes precedence over global variable 'm'
}

int main()
printf("%d",m);        --> Output: 10
function1();
}


3) Static variable: It is declared with the keyword 'static'. The scope of this variable is within the function in which it is declared. Lifetime is throughout the program (ie., it retains its value throughout the life of the program). It is initialized only once.

Example:

void function1()
{
static int n=0;
n++;
printf("%d1n",n);
return;
  }

int main()
{
int i;
for(i=0;i<3;i++)
funtion1();
getch();
return 0;
}

 

Output:

1
2
3

The function is called thrice from the main.  When 'n' is declared as static, it is initialized to zero only for the 1st call. For the second call, 'n' retains its previous value 1 and so it is incremented to 2 and then to 3 for the third call.

 

4) Register variables: The variable name should be preceded by the keyword 'register'. The values of these variables are stored in CPU register instead of computer memory. So, they can be accessed faster and hence execution time is reduced. But only a few variables can be stored inside registers.

 

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error