Storage Classes in C Language with Examples

This C Tutorial explains Storage Class in C Language as well as their types. It also explains the relationship with the scope of the variables/identifiers in the program with examples.

Storage class of a variable refers to the type of memory allocated to a variable. For example

int num = 10;    /* num, an integer is allocated the automatic memory */

There are four types of storage classes viz. automatic, registers, static and global or extern.

Variables which are declared inside the blocks, inside the curly braces {}, are called the local variables and are given automatic storage. For example:

    int index;                    
    char colour;
 
    /*
     * index is an automatic variable, automatic keyword is not necessary.
     * colour is also an automatic variable of type character, characters are internally INTEGERS
     */

Register variables store values in the hardware registers. However they behave exactly as the automatic variables in that they are created and destroyed when the function is called & exited respectively. For example:

advertisement
advertisement
    register int x = 10;

When a variable is declared static, it is given memory allocation, at the compile time, in the static area before the program begins to execute and exists throughout the execution of the program. Also, static variables retain their last modified values. Variables declared outside of any blocks are allocated statically. When declared inside some function, exists during entire program for that particular function whenever called. Static variable is initialized to 0 by default! For example:

     static int sum = 0;

Global variables are declared outside of all functions. They are also allocated memory statically. Remain in existence during the entire execution of the program. Initialized to 0 by default. If they are to be accessed across other source files than the file in which they are defined, use extern keyword.

Relation of Storage Class to Scope of the Variables

Automatic and register variables have their life from the point of declaration till the end of the block in which they are declared!

advertisement

Static variables have their scope from the point of declaration till the end of the file! In cases, if defined inside a block, their access is within the block!

Global variables can be accessed throughout the source file from their declaration and across other source files with extern keyword.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

advertisement
If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.