0% found this document useful (0 votes)
7 views4 pages

l3 - Storage Classes

The document explains storage classes in C language, which specify how and where variables are stored, their initial values, and their scope. It describes four types of storage classes: Automatic (auto), Register (register), Static (static), and External (extern), detailing their features such as storage location, default initial values, scope, and lifetime. Examples are provided for each storage class, illustrating their usage in code.

Uploaded by

bssphoenixphp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

l3 - Storage Classes

The document explains storage classes in C language, which specify how and where variables are stored, their initial values, and their scope. It describes four types of storage classes: Automatic (auto), Register (register), Static (static), and External (extern), detailing their features such as storage location, default initial values, scope, and lifetime. Examples are provided for each storage class, illustrating their usage in code.

Uploaded by

bssphoenixphp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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).
Intial value :-what would be the initial value of the variable.
If initial value not assigned, then what value taken by uninitialized variable.

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

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);

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.

Variable stored in register storage class always access faster than,which is always stored in the
memory. But to store all variable in the CPU register is not possible because of limitation of
the register pair.

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);
}

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.


Declaration does not create variables, only it refer that already been created at
somewhere else. So, memory is not allocated at a time of declaration and the 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.

You might also like