Ticker

6/recent/ticker-posts

Storage classes in c language

Storage Classes 

Storage class in c language is a specifier which tells the compiler where and how to store variables, its initial value and scope of the variables in a program. Or attributes of variable is known as storage class or in compiler point of view a variable identify some physical location within a computer where its string of bits value can be stored is known as storage class. 

The kind of location in the computer, where value can be stored is either in the memory or in the register. There are various storage class which determined, in which of the two location value would be stored.

Syntax of declaring storage classes is:-

storageclass     datatype     variable name; 

There are four types of storage classes and all are keywords:- 

1) Automatic (auto) 

2) Register (register) 

3) Static (static) 

4) External (extern) 

Examples:- 

    auto float x; or float x; 

    extern int x; 

    register char c; 

    static int y; 

Compiler assume different storage class based on:- 

1 ) Storage class:- tells us about storage place(where variable would be stored). 

2) Intial value :-what would be the initial value of the variable. If initial value not assigned, then what value taken by uninitialized variable. 

3) Scope of the variable:-what would be the value of the variable of the program. 

4) Life time :- It is the time between the creation and distribution of a variable or how long would variable exists. 

1. Automatic storage class 

The keyword used to declare automatic storage class is auto. 

Its features:- 

Storage-memory location 

Default initial value:-unpredictable value or garbage value.

Scope:-local to the block or function in which variable is defined. 

Life time:-Till the control remains within function or block in which it is defined. It terminates when function is released. 

The variable without any storage class specifier is called automatic variable. 

Example:- 

main( ) 

    auto int i; 

    printf(“i=”,i); 

}

2. Register storage class 

The keyword used to declare this storage class is register. 

The features are:- 

Storage:-CPU register. 

Default initial value :-garbage value 

Scope :-local to the function or block in which it is defined. 

Life time :-till controls remains within function or blocks in which it is defined. 

Register variable don’t have memory address so we can’t apply address operator on it. CPU register generally of 16 bits or 2 bytes. So we can apply storage classes only for integers, characters, pointer type.

And when variable is used at many places like loop counter, then it is better to declare it as register class. 

Example:- 

main( ) 

    register int i; 

    for(i=1;i<=12;i++) 

        printf(“%d”,i); 

 3 Static storage class 

The keyword used to declare static storage class is static. 

Its feature are:- 

Storage:-memory location 

Default initial value:- zero 

Scope :- local to the block or function in which it is defined. 

Life time:- value of the variable persist or remain between different function call. 

Example:- 

main( ) 

    reduce( ); 

    reduce( ); 

    reduce ( ); 

reduce( ) 

    static int x=10; 

    printf(“%d”,x); x++; 

Output:-10,11,12 

External storage classes

The keyword used for this class is extern. 

Features are:- 

Storage:- memory area 

Default initial value:-zero 

Scope :- global 

Life time:-as long as program execution remains it retains. 

external variables are declared at outside of all the function. 

Example:- 

 int i,j; 

void main( ) 

        printf( “i=%d”,i ); 

        receive( ); 

        receive ( ); 

        reduce( ); 

        reduce( ); 

receive( ) 

        i=i+2; 

        printf(“on increase i=%d”,i); 

reduce( ) 

        i=i-1; 

        printf(“on reduce i=%d”,i); 

Output:-i=0,2,4,3,2.

When there is large program i.e divided into several files, then external variable should be preferred. External variable extend the scope of variable.

ALSO SEARCH:

"how many storage classes in c"

"auto storage class in c"

"external storage class in c"

"static storage class in c example"

"storage classes in c pdf"

"extern storage class in c example"

"storage classes in c"

"auto storage class in c"

"external storage class in c"

"static storage class in c example"

"how many storage classes in c"

"extern storage class in c example"