Docsity

Storage classes in c, Lecture notes of C programming

Partial preview of the text

Download Storage classes in c and more Lecture notes C programming in PDF only on Docsity!

Stora

ge

Meaning of Storage

Each variable declared^ Class in C contains

not only its data type but also it has a

storage class specified with it.

If user do not specify the storage class

of a variable , the compiler will assume

its storage class as default i.e.

automatic.

Purpose of Storage

The storage class of a variable tells us about Class :-

  • (^) Storage Place of Variable i.e.

Memory or CPU Registers

  • (^) Initial value of Variable
  • (^) Scope of Variable
  • (^) Lifetime of Variable i.e. how long

variable exists.

How Storage Class

Declared ????Storage class Datatype

variable name;

auto int a ,b ;

Storage

Class DataType Variable

Automatic Storage

Class

Character

istics

Meaning

Storage Memory Initial Value Garbage Value i.e. An Unpredictable Value. Scope or Visibility Local or Visible in the Block in which it is declared. Life Time It retains its value till it is in the

This is the default storage class for all the

variable. It always reinitialize the value

of variable .It is declared as : -

auto int a ;

Void main ( ) { auto int i , j = 5 ;

Automatic Storage Class^ int k^ ,m =10 ;^ // By Default

of j = % d^ printf ( “ value of “ , i , j ) ;^ i^ = % d^ \n value

of m = % d^ printf ( “ value of “ , i , j ) ;^ k^ = % d^ \n value }

Value of i = 2009 Value of j = 5 Value of k = 1005 Value of m = 10

Garbage Value Variable Value

Use of Automatic

Storage Class

Use of Automatic

void ck ( ) ; void main( ) Storage Class

clrscr ( ) ; ck ( ) ; ck ( ) ; ck ( ) ; getch( ) ; }

void ck ( ) { int i = 0 ; %d”,i ) ;^ printf ( “\n\n Value of I .. i + + ; } (^) Output 0 0 0

Register Storage

Class

Characteri

stics

Meaning

Storage C P U Registers
Initial Value Garbage Value
Scope Visibility or Local to the Block in which it is
declared

In this storage class , variable is stored in C P U Registers , just for the sake of increase the execution speed of some variable of

program. It is declares as :-

register int a ;

Static Storage

This storage class is used when a user want that a variable should Classretain its value even

after the execution of the function in which it is

declared, then this storage class is used. It is

declared as follow :- static int

a ;

Characteri

stics

Meaning

Storage Memory Initial Value Zero ( 0 ) Scope or Visibility Local to the block in which it is declared Life Time It retains its value between the

Use of Static Storage

void ck ( ) ; void main( ) Class

clrscr ( ) ; ck ( ) ; ck ( ) ; ck ( ) ; getch( ) ; }

void ck ( ) { static int i = 0 ; %d”,i ) ;^ printf ( “\n\n Value of I .. i + + ; } (^) Output 0 1 2

16

Examples (internal static)

Internal static variable can be used to count function. eg. the number of calls made to int { main() int for(i I; =1; i<=3; i++) }^ stat(); void { stat() static x = x+1; int x=0; }^ printf(“x^ =^ %d\n”,x);

Output x= x= x=

17

External static variables

An external static variable is declared outside of all functions and is available to all the functions in the program. An external static variable seems similar simple external variable but their difference is that static external variable is available only within the file where it is defined while simple external variable can be accessed by other files.

External Storage

Class

Characteri

stics

Meaning

Storage Memory Initial Value Zero ( 0 ) Scope or Visibility Global ( Visible in all the Program ) Life Time It retains its value through out the

External variables are declared outside all

functions i.e, at the beginning of the program.

Global variables should be available to all the

functions with the help of extern specifier. It

is declared as follow : - extern int a ;

20

External declaration(examples) int { main() extern... int y;

}^.^.^. func1() { extern... int y;

}^..^. int y;

Note that extern declaration does not allocate storage space for variables