C Storage Class Specifiers – auto, register & extern

C storage class specifiers determines the physical location in memory within the computer where the variable declared in c would be actually stored.

Storage class specifiers also determines

  • The initial value of the variable, if an initial value is not assigned.
  • The life of a variable.
  • The scope of the variable.

Available c storage class specifiers in the c programming language are as follows:

auto Storage Class Specifier

The variables declared in c using auto storage class are initialized with an unpredictable initial value often called as garbage value and can be accessed from anywhere within the block in which the variable is declared. As soon as the program control exits from the block in which it is defined, the variable is destroyed.

By default variable in c use the auto storage class.

Syntax
auto data_type variable_name;

For example,

#include<stdio.h>
main()
{
    auto int i=1;
    {
        {
            {
                printf("n%d",i);
            }
            printf("n%d",i);
        }
        printf("n%d",i);
    }
    return 0;
}

register Storage Class Specifier

The variables declared using register storage class specifier are treated similarly like that defined by auto storage class specifier with the only difference is that the variables are stored within the CPU registers providing faster access.

It is recommended to use register storage class for variables which are being used at many places.

Syntax
register data_type variable_name;

For example,

#include<stdio.h>
main()
{
    register int i;
    for(i=1; i<=100; i++)
        printf("n%d",i);
    return 0;
}

static Storage Class Specifier

The variables declared with static storage class specifier are initialized with zero initial value if any initial value is not provided at the time of declaration and it can be accessed from anywhere within the block in which it is defined.

However, the variables declared with static storage class are not destroyed even after program control exits from the block. Thus, the value of the variable persists between different function calls.

Syntax
static data_type variable_name;

For example,

#include<stdio.h>

increment()
{
    static int i=1;
    printf("%dn",i);
    i++;
}
main()
{
    increment();
    increment();
    increment();
    return 0;
}

Output
1
2
3

Extern Storage Class Specifier

The variable declared using extern storage class are stored in memory with by default zero initial value and continue to stay within the memory until the program’s execution is not terminated.

Moreover, variables declared as extern can be accessed by all functions in the program, thus avoiding unnecessary passing of these variables as arguments during function call.

It should be noted that the variables declared outside any function definition are treated as variables with extern storage class.

Syntax
extern data_type variable_name;

For example,

#include<stdio.h>

int i;
main()
{
    printf("ni=%d",i);
    increment();
    increment();
    decrement();
    decrement();
}
increment()
{
    i++;
    printf("nOn increment, i=%d",i);
}
decrement()
{
    i--;
    printf("nOn decrement, i=%d",i);
}

Output
i=0
On increment, i=1
On increment, i=2
On decrement, i=1
On decrement, i=0

4 Responses

  1. Weston Humphrey
    Weston Humphrey August 18, 2012 at 1:48 am | | Reply

    I think this is a real great blog article.Much thanks again. Will read on…

  2. Geetansh
    Geetansh December 20, 2012 at 11:29 am | | Reply

    Thanks A Million!!

  3. RK Khalsa
    RK Khalsa July 6, 2013 at 2:49 pm | | Reply

    thanx a lot,…..

  4. sravya
    sravya December 24, 2013 at 5:47 am | | Reply

    wow its really great site i good n clear examples n last but not the least thanks a lot…………………:-)

Please comment with your real name using good manners.

Leave a Reply