You are on page 1of 10

Storage Classes

Two kinds of storage locations. Memory and CPU registers.

Variables storage class tells


Storage location Initial value Scope of the variable Life of the variable

Four storage classes


auto register static external

auto
Storage Location - Memory

Initial Value Scope


Life

- Garbage Value - Local to the block in which the variable is defined. - Till the control remains within the block in which the variable is defined.

Implicitly all the variables are auto storage class.

Example: auto int a,b; #include<stdio.h> #include<conio.h> main() { auto int a; clrscr(); printf (%d,a); getch() }

register
Storage Location CPU register Initial Value Scope Life - Garbage Value - Local to the block in which the variable is defined. - Till the control remains within the block in which the variable is defined.

A value stored in a CPU register can be accessed faster than the one which is stored in memory.

Example: register int a,b; #include<stdio.h> #include<conio.h> main() { register int a; clrscr(); printf(%d,a); getch() }

static
Storage Location - Memory Initial Value - Zero Scope - Local to the block in which the variable is defined. Life - value of the variable persists b/w different function calls.

Static variables are maintaining value between different function calls.

Example: static int a,b; #include<stdio.h> #include<conio.h> main() { increase(); increase(); increase(); } increase() { static int a=1; printf(%d,a); a=a+1; getch(); }

external
Storage Location Initial Value Scope Life - Memory - Zero - Global - As long as the programs execution doesnt come to an end.

Example: external int a,b; #include<stdio.h> #include<conio.h> external int a; main() { clrscr(); printf(%d,a); getch() }

You might also like