Storage classes

The storage class of a variable tells the compiler,

Types of storage classes

There are four types of storage class in c.

Note:

[quote]automatic, static, and external storage classes are kept memory type.[/quote]

Automatic storage class

Automatic variables are declared inside a function in which they are to be utilized. They are created when the function is called and destroyed automatically when the function is exited; hence the name automatic variables are private (local) to the function in which they are declared.

int main ()
{
        auto int a;
        ……;
        ……;        return 0;
}
int main ()
{
       int a;
        ……;
        ……;      return 0;
}

A variable declared inside a function without storage class specification is by default an automatic variable.

We may also use the keyword auto to declare an automatic variable.

Note:

External storage class

A variable that is both alive and active throughout the program is known as an external variable. They are also known as a global variable can be accessed by any function in the program. An external variable is declared outside a function.

Once a variable has been declared as global, any function can use it and change its value. Then, subsequent functions can reference only that new value.

One other aspect of a global variable is that it is available only from the point of declaration to the end of the program.

void fun();
void main ()
{
      y=5;
       …;
       …;
}
int y;
void fun ()
{
    y=y+1;
      …..;
}

We have a problem in above program. As far as main is concerned, y is not defined. So, the compiler will issue an error message.

void fun();
void main()
{
        extern int y;
        y=5;
           …;
           …;
}
int y;
void fun ()
{
    y=y+1;
         …..;
}

If you want to access a global variable into our main() function by using the keyword extern; i.e. external declaration.

Note:

Static storage class

As the name suggests, the value of static variables persists until the end of the program. A variable can be declared static using the keyword static.

A static variable may be either an internal type or external type depending on the place of declaration. An internal static variable is those, which are declared inside a function, the scope of internal static variable, extend up to the end of the function in which they are defined. Therefore, internal static variables are similar to auto variables, except that they remain in existence throughout the program.

An external static variable is declared a variable is declared outside of all functions and is available to all the functions in the program. The difference between a static external variable and external variable is that the static external variable is available only within the file where it is defined while the external variable can be accessed by other files.

A static variable is initialized only once when the program is compiled. It is never initialized again.

Note:

Example:

#include <stdio.h>
void state();
int main()
{
    int i;
    for (i=0 ; i<=3 ; i++ )
    state();
    
    return 0;
}

void state()
{
    static int x; 
    x=x+1;
    printf("\nx= %d",x);
}

Output:

[quote]

x= 1                                                                                                                                                         
x= 2                                                                                                                                                         
x= 3                                                                                                                                                         
x= 4  

[/quote]

Register storage class

It is created at the nearest memory available to the CPU named as register. A value stored in a CPU register can always be accessed faster than the one that is stored in memory.

Limitations

[quote]

[/quote]

Note

[quote]

[/quote]

Global variable Vs Local variable

Global Variable

Local variable

  • The variable which is declared above any function is called global variables
  • These variables can be accessed by all the functions in the program
  • The default value for the global variable is zero.
  • The variables which are declared within any function are called local variables.
  • These variables can e accessed only by the function in which they are declared
  • The default value for the local variable is garbage value.

Statis Vs automatic variables

Variable

Storage class

Keyword

Storage Location

Scope

Extent

Initial value

Automatic variables

Automatic

Auto

Memory

Local to the block or function in which the automatic variable is declared

The automatic variable will be alive during the execution of block or function in which it is declared.

Garbage

Static variables

Static

Local static

Global static

Memory

Memory

Local to the block or function in which static variable is declared

Accessible from the place of the declaration to the entire program

It is alive between two function calls.

It is alive during the execution of the entire program.

Zero

Zero