C storage classes

C storage classes

In additional to data type, every variable has one or more attributes known as storage class or storage class are used to determine the memory location, visibility and the default or initial value of the variable.
we can specify storage class when declaring a variable.

The general syntax

Storage_class_name Data_type variable_name;

There are four types of storage classes in c language

.

1. Automatic
2. External
3. static
4. Register

with the use of storage classes keywords auto, static, register, extern. so we can write declaration statements like this

auto int a,b;
static float f;
register int z;

When explicitly the storage class specifier is not present in declaration, compiler automatically assume the default storage classes on the based on declaration.

A storage class are determine about these four aspects of a variable

1 Scope The Loaction where the variable is stored or avialable to use
2 initial value Default value when variable not initialize
3 place of storage place in memory where the storage for variable.
4 Lifetime This is time between the creation and destruction of variable

Automatic

when all the variable declare any where inside a function/block without any storage class specifier are automatic variable. we can also use auto keyword to declare automatic variables. in this example both function have local variables both are equals

void fun1()
{
int x,y;
……
……

}
void fun2()
{
auto int x,y;
……
……

}

Register

The register storage class can only apply on automatic variables. The scope of variable is lifetime and initial value of register variable is same as automatic variable. but only difference between automatic and register variable is in the place where it is stored. Automatic variables are stored in memory but the register variables are stored in cpu registers, but registers can stored only small memory in the processor. The variables stored in the register can be accessed much faster than the variables stored in memory

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

But Register variables do not have memory address so we cannot apply (&) operator to find the address of register variable.

External

Variables that have to be use by many functions and also use by different files can be declare as external variables. The default value of external variable is zero.

extern int age;

Definition of an external variable

1. The definition create the variable, so at the definition time memory is allocated to the variable.
2. There can be only one definition.
3. The definition can be written only outside function.

Declaration of an external variable

1. Declaration can not create variable so at the time of declaration memory can not allocated.
2. external variable can be many declaration.
3. The extern keyword always specified at the time of declaration.

#include< stdio.h >
int a=9;
void main()
{
……..
……..

}
void func1()
{
………
………

}
void func2()
{
………
………

}

In this above example the variable a avalaibe for all functions