Signup/Sign In

Storage Classes in C language & How to Use them

Posted in Programming   LAST UPDATED: FEBRUARY 11, 2020

    In the computer world, C language is considered as the benchmark for understanding the potential for aspiring software professionals. All the interviews on-campus or off-campus end up having a programming or coding test in C language invariably. Hence every aspiring software engineer needs to master the C language and its concepts. One such concept in Frequently asked questions (FAQs) of C coding test is Storage Classes. This article is specifically for those who want to refresh their concepts of programming for job interviews or coding tests.

    Let us understand storage classes in little detail. There are 2 storage classes in C language, based on it the variable is either Local variable or global variable.




    Local Variable C Language:

    Let's take an example of local variable usage in C language,

    #include<stdio.h>
    #include<conio.h>
    
    void main ()
    {
        int lv = 10;   // local variable declaration
        printf("value of lv = %d\n", lv);
        getch();
    }



    Global Variable in C Language:

    Let's take an example of global variable usage in C language,

    #include<stdio.h>
    #include<conio.h>
    
    int gv;   // global variable declaration
    
    void main ()
    {
        int a, b;   //local variable declaration
        a = 10;   //Data initialisation
        b = 20;
        gv = a + b;
    
        printf("value of a = %d, b = %d and gv = %d\n", a, b, gv);
        getch();
    }



    Difference between Local and Global variables

    The Local variables are identified to have the following characteristics:

    • They are unknown to the functions (other than where they are declared), and to the main function too.

    • These variables have limited accessibility, i.e. within the declared function.

    • Stack memory is allocated for the local variables & these stacks are freed up after the variable usage is over within the program.

    • They are recreated each time a function is executed or called.

    • Their initiation is not automatic.

    • Their lifetime and scope are limited to the function where they are declared.

    Now let us understand Global Variables in C language context:

    • These variables are accessible by all the functions within the program.

    • These variables are visible throughout the program from the point of their declaration.

    • They are not recreated on recalling the function (Hence memory operations are reduced).

    • To declare a global variable, we have to declare it outside all the functions (including main).

    • Memory allocation for global variables is on the data segment.

    • The initialization of global variables is set to 0 unless explicitly defined or declared.

    • In the case of the name match for a local and global variable, the function will use the local variable over global, ignoring the global variable for that very function.

    In C language, there is also a static variable declaration. The storage class, static has a lifetime equal to that of the program. Static storage class is specified for automatic as well as global variables. The allocation of memory for static variables is done on the heap. The scope of a static global variable is only limited to the file in which it is used or declared. Hence, a user cannot use extern in a different file and access the static global variable.

    Now, it is also important for budding programmers to understand the major differences between static and global variables. Though at the first based on the declaration and usage they may appear to be the same but are majorly different.

    Global variables persevere throughout the program, and the scope is also throughout the program. It means that global variables can be accessed from any function within the program. Whereas Static Variables are local in scope to the block or file in which they are defined, but their lifespan is throughout the program.

    About the author:
    Navjyotsinh Jadeja is Faculty in an Engineering College, driving cross-disciplinary research and innovation in technology, sciences. He is the Pedagogical Innovation Award Winner from Gujarat Technological University Innovation Council.
    Tags:CStorage Class
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS