Home > Programming In C > C-Storage Classes

C-Storage Classes

C-Storage Classes – There Are Some Keywords That Are Used In Storage Classes. Storage Means Saving The Data In Memory, Memory May Be A Main Memory Or Secondary Memory Or It May Be Some Registers.

There Are Different Storage Classes In C-Langauge.

There Are 4 Storage Classes Specifies Supported In C.

  1. Auto
  2. Extern
  3. Static
  4. Register

Auto – Auto Is Nothing But It Is Automatic Variable Or It Is Also Called As Local Variable.

We Use The Keyword ‘auto’

Whenever A Variable Or A Datatype Is Specified With Name Auto It Means It Is Local Variable Means The Data Will Be Stored Which Will Be Accessible Only For Local Variables.

The Scope Of This ‘auto‘ Is Available Only Within The Block In Which It Is Declared.

Only The Variable Declared Within The Block Can Access This auto

Example –

#include <stdio.h>

main()

{

auto int i=1;

{

auto int i=2;

{

auto int i=3;

printf(“%d”,i);

}

printf(“%d”,i);

}

printf(“%d”,i);

}

 

Output Will Be 

3

2

1

 

Extern – The Extern Variable And It Is Also Called As Global Variable.

Keyword We Use For This Variable Is extern

Scope Of This Variable Is It Is Available Through Out The Program, They Are Declared Outside The Block.

So These Global Variables And Extern Variables Are Declared Outside The Block.

 

The Default Value For Automatic Or Local Variables Will Be The Garbage Value, Means It Prints Any Value But For Extern Or Global Variables The Default Value Is Zero.

Program For Extern Storage Class:-

#include <stdio>

extern int i=1; // it is available through out the program //

main()

{

int i=3; // this is local variable and only accessable in main block

printf(“%d”,i);

fun();

}

fun()

{

printf(“%d”,i);

}

Output

3

1

Explanation – before writing the program, i have to declare the extern variable the global variable and that variable can be accessed in complete program i.e the value is 1

inside main block there we declared local variable the value is 3

i’m using fun() call, this jumps to the fun() call and prints the what is declared after fuction call , there it prints the global variable i.e  the value is 1

Static Variable :- 

The Keyword For This Static Storage Class Is static

The Scope Of This Variable Retains Its Value Through Out The Program Just Like Global Variables And Also Inbetween Function Calls.

Static Variables Will Be Initialized Only Once.

Default Value – Zero For This Static Variable

Program For Static Variable:-

#include <stdio.h>

main()

{

inc(); // inc is the function call //

inc();

inc();

}

inc()

{

static int i=1;

printf(“%d”,i);

i++;

}

Output

1

2

3

Explanation – inc () function is present outside the block, the definition of static this variable retains its value through out the program & in between fuction calls.

first it reads inc() in the main,

its control jumps to the inc() definition which is present outside the main.

its see static int i=1, it prints 1

now the i value is incremented because we declared i++(incrementation)

now the second function call in the main function i.e inc()

then again it comes outside the main block to the inc() definition and enters into the definition and i is incremented and it prints 2

then again the control goes to main block inc() function and comes again to the inc() fuction call definition which is present outside the mainblock and i value is incremented and prints 3

1

2

3

are the output.

only three function calls are there in the main block, if the i++ incremental function is not there the output will be

1

1

1

Register Variable – Register Is the Memory Which Is Present In CPU, It Is Fast Storage Device, Which Accesses Those Values Which The Processor Wants To Execute.

Within The CPU There Is Processor In The Processor There Is Register, CPU Takes The Data From Registers And Place The Result First In Register And After It Sends To Input-Output Devices.

Register Is Small Storage Memory But It Is Very Fast Memory Fast Accessing Will Be There For The Registers.

Keyword – Keyword For Register Is register

Register Variable Values Are Stored In CPU Register Rather Than In Memory.

Registers Are Temporary Storage Unit In CPU.

If You Close The Window You Will Loose The Data.

They Allow Faster Access Time For Register Variables Than Normal Variables.

 

Program For Register Variable

#include <stdio.h>

main()

{

register int i;

for(i=1; i<=5; i++)

{

printf(“%d”,i);

}

}

OUTPUT

1

2

3

4

5

Explanation – upto 5 the values will be printed and these values will be stored in cpu registers rather than in memory and this program will execute faster than other program because we declared register variable.

 

 

 

 

 

 

Leave a Reply

Secured By miniOrange