0 votes
195 views
in C programming by (98.9k points)
edited
Explain the storage classes with example.

1 Answer

0 votes
by (98.9k points)
selected by
 
Best answer

The different locations in the computer where we can store data and their

accessibility, initial values etc. very based on the way they are declared. These

different ways are termed as different storage classes.

• In C there are for storage classes, namely

1. Automatic

2. Register

3. Static

4. External or global

• Let us see these storage classes one by one

1. Automatic storage class

In this case data is stored in memory

The initial value of such a variable is garbage

The scope of the variable is local i.e. limited to the function in which it is defined.

The life of such variables is till the control remains in the particular function where

it is defined.

For e.g.:

Int i; or auto int i;

2. Register storage class

In this case data is stored in CPU register

The initial value of such a variable is garbage.

The scope of the variable is local i.e. limited to the function in which it is defined

The life of such variables is till the control remains in the particular function where

it is defined.

For e.g.:

Register int I;

In this case the data is stored in a small memory inside the processor called its

registers.

The advantage of such storage class is that since the data is in the processor itself,

its access and operation on such data is faster.

There is limitation on the size of the data that can declared to be register storage

class. The data should be such that it doesn’t require more than 4 bytes. Hence

double and long double data types cannot be declared as a register.

Also there is a limitation on the maximum number of variables in a function that

can be a register class. The limitation is that a maximum of 3 register class variable

can be declared in a function.

3. Static storage class

In this case data is stored in a memory

MUQuestionPapers.com 13

The initial value of such a variable is zero

The scope of the variable is local i.e. limited to the function in which it is defined

The life of such variable is till the program is alive.

For e.g.:

Static int I;

If a variable is declared static, its value remains unchanged even If the function

execution is completed.

When the execution to that function returns, the previous value is retained.

Thus it says the initialization is only once. If you have an initialization statement of a

static member, it will be executed only once i.e. for the first time when this

function is called.

4. External or global storage class

In this case data is stored in memory

The initial value of such a variable is zero.

The scope of the variable is global i.e. it is accessible from anywhere in the

program.

The life such a variable is till the program is alive.

Related questions

0 votes
1 answer 124 views
asked Jun 11, 2022 in C programming by Doubtly (98.9k points)
0 votes
1 answer 149 views
0 votes
0 answers 78 views
asked May 28, 2022 in C programming by Doubtly (98.9k points)
0 votes
1 answer 151 views

Doubtly is an online community for engineering students, offering:

  • Free viva questions PDFs
  • Previous year question papers (PYQs)
  • Academic doubt solutions
  • Expert-guided solutions

Get the pro version for free by logging in!

5.7k questions

5.1k answers

108 comments

563 users

...