Storage Classes in C

Previous Next Article



What are Storage Classes ?

Storage Classes in C provides the way how to store variables in memory. There are various c storage classes available as follows ,

1) auto

2) static

3) register

4) extern

1) auto

The auto keyword is applied to all local variables automatically. It is the default storage class that is why it is known as automatic variable. It's default value is Garbage Value.

Example :-

void main()
{
  int b = 5;
  auto int a = 5;     // Same as above . Auto keyword is default keyword. 
  printf(" %d, %d ", a,b);
}

Output :-

5 , 5

Auto keyword is present by default. No matter whether you write it or not. It stores the value in RAM.

2) static variables in c

static variables are initialised only once in a lifetime of program. That means it gets initialsed only once when function is called . It's default value is zero.

Example

#include <stdio.h>
void function()
{
    static int a = 0; /* This statement gets initialised only once then it is ignored. */
    int b = 0;
    
    a++;
    b++;
    printf("\na = %d , \nb = %d",a,b);
}

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

Output

a = 1 , 
b = 1
a = 2 , 
b = 1
a = 3 , 
b = 1

Firtsly main Function is called. Then at the first execution of "function()" static variable 'a' becomes 0 . Then it increments it's value by 1. Then in second execution of "function" static variable 'a' value is 1 not 0. Because it gets initialised only once in a lifetime always remember. Then in third execution of "function()" value of static variable becomes '3' .

3) register

register variables are mostly used in places where we need fast execution . Bcause it is stored in register and registers are always fast than RAM. It is mostly used in incrementing the counter or in loops.

register int counter=0;  

4) extern

Consider this example for better understanding of extern keyword..

Example :-

Create two files Demo.c and NewDemo.c In Demo.c
#include <stdio.h>
#include "NewDemo.c" 

 int num =  75 ;  
 extern void display();

int main()
{
display();
 return 0;
}
In NewDemo.c
#include <stdio.h>

extern int num;
void display()
{
 printf("Num is = %d\n",num );
}

Output after compiling and running demo.c :-

Num is = 75

Entry point in demo.c is main function.

Inside main function we called display() function. And this display function is not defined in demo.c if you look at the program carefully.

display() function is defined in NewDemo.c .

So you have to tell the complier that display() function is defined somewhere else and I have to use it. So declare extern void display() before main function in demo.c This extern keyword tells the compiler that its okay if this program demo.c do not contains the definition of display() function . I will search it somewhere else. Just provide me the name of file where it is being defined.

In this case we included #include “NewDemo.c”

Now in NewDemo.C

display() function is defined here .

This function prints the value of num. But if you see num is not initialised with any value in this program . Yes , if you see in NewDemo.c , num is not defined anywhere.

So you have to tell the compiler that this variable num is defined somewhere else. So declare this variable as extern int num at top of program outside the function.

In this way ,

extern is used to tell the particular program that variable or function is not present in this program . So search it somewhere else. But do not forget to include the name of file where that extern variable or function is defined.

In this case we included #include “newDemo.c” in demo.c

Solve the Quiz

1) Is Java Architecture-Neutral language?
Yes
No

2) Can you use the compiled Java code of Windows machine in Linux machine? (Assume JVM is installed in both machines)
Yes
No

Previous Next Article






codzify.com


Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.

Social Profile


Linkedin
Twitter
Facebook

Copyright 2018. All rights reserved.