0% found this document useful (0 votes)
9 views3 pages

Storage Class in C

The document explains the four storage classes in C programming: Automatic, Register, External, and Static. Each storage class defines the memory allocation, lifetime, and scope of variables, with Automatic and Register being local to blocks, while External and Static can have global scope. It emphasizes the importance of careful use of External variables to maintain modular programming practices.

Uploaded by

gia.143.epi
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)
9 views3 pages

Storage Class in C

The document explains the four storage classes in C programming: Automatic, Register, External, and Static. Each storage class defines the memory allocation, lifetime, and scope of variables, with Automatic and Register being local to blocks, while External and Static can have global scope. It emphasizes the importance of careful use of External variables to maintain modular programming practices.

Uploaded by

gia.143.epi
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/ 3

Asansol Rainbow Computer Centre

Pravat Sinha. Mob-9091454548

Storage Classes
Every C variable has a storage class and a scope. The storage class determines the part of memory where
storage is allocated for an object and how long the storage allocation continues to exist. It also determines
the scope which specifies the part of the program over which a variable name is visible, i.e. the variable is
accessible by name. There are four storage classes in C are

1. Automatic,
2. Register,
3. External,
4. Static.

Automatic Variables

They are declared at the start of a block. Memory is allocated automatically upon entry to a block and freed
automatically upon exit from the block. The scope of automatic variables is local to the block in which they
are declared, including any blocks nested within that block. For these reasons, they are also called local
variables. No block outside the defining block may have direct access to automatic variables, i.e. by name. Of
course, they may be accessed indirectly by other blocks and/or functions using pointers.

Automatic variables may be specified upon declaration to be of storage class auto. However, it is not
required; by default, storage class within a block is auto. Automatic variables declared with initializers are
initialized each time the block in which they are declared is entered.

auto int x=10;

Register Variables

Register variables are a special case of automatic variables. Automatic variables are allocated storage in the
memory of the computer; however, for most computers, accessing data in memory is considerably slower
than processing in the CPU. These computers often have small amounts of storage within the CPU itself where
data can be stored and accessed quickly. These storage cells are called registers.

Normally, the compiler determines what data is to be stored in the registers of the CPU at what times.
However, the C language provides the storage class register so that the programmer can ``suggest'' to the
compiler that particular automatic variables should be allocated to CPU registers, if possible.
Thus, register variables provide a certain control over efficiency of program execution. Variables which are
used repeatedly or whose access times are critical, may be declared to be of storage class register.

Register variables behave in every other way just like automatic variables. They are allocated storage upon
entry to a block; and the storage is freed when the block is exited. The scope of register variables is local to
the block in which they are declared. Rules for initializations for register variables are the same as for
automatic variables.

Register int x=10;


Asansol Rainbow Computer Centre
Pravat Sinha. Mob-9091454548
External Variables

All variables we have seen so far have had limited scope (the block in which they are declared) and limited
lifetimes (as for automatic variables). However, in some applications it may be useful to have data which is
accessible from within any block and/or which remains in existence for the entire execution of the program.
Such variables are called global variables, and the C language provides storage classes which can meet these
requirements; namely, the external and static classes.

External variables may be declared outside any function block in a source code file the same way any other
variable is declared; by specifying its type and name. No storage class specifier is used - the position of the
declaration within the file indicates external storage class. Memory for such variables is allocated when the
program begins execution, and remains allocated until the program terminates. Fo rmost C implementations,
every byte of memory allocated for an external variable is initialized to zero (0).

#include<stdio.h>

void abc();

int x=10; //External Variables. Global Scope

void main()

External variables may be initialized in declarations just as automatic variables; however, the initializers must
be constant expressions. The initialization is done only once at compile time, i.e. when memory is allocated
for the variables variables.

In general, it is a good programming practice to avoid use of external variables as they destroy the concept of
a function as a ``black box''. The black box concept is essential to the development of a modular program with
modules. With an external variable, any function in the program can access and alter the variable, thus
making debugging more difficult as well. This is not to say that external variables should never be used. There
may be occasions when the use of an external variable significantly simplifies the implementation of an
algorithm. Suffice it to say that external variables should be used rarely and with caution.

Static Variables

As we have seen, external variables have global scope across the entire program
(provided extern declarations are used is files other than where the variable is defined), and a lifetime over
the entire program run. The storage class, static, similarly provides a lifetime over the entire program,
however; it provides a way to limit the scope of such variables, Static storage class is declared with the
keyword “static” as the class specifier when the variable is defined. These variables are automatically
Asansol Rainbow Computer Centre
Pravat Sinha. Mob-9091454548
initialized to zero (0) upon memory allocation just as external variables are. Static storage class can be
specified for automatic as well as external variables.

Static automatic variables continue to exist even after the block in which they are defined terminates. Thus,
the value of a static variable in a function is retained between repeated function calls to the same function.
The scope of static automatic variables is identical to that of automatic variables, i.e. it is local to the block in
which it is defined; however, the storage allocated becomes permanent for the duration of the program.
Static variables may be initialized in their declarations; however, the initializers must be constant expressions,
and initialization is done only once at compile time when memory is allocated for the static variable.

static int x=10;

--

#include<stdio.h>
int abc()
{
static int c = 0;
c=c+1;
return c;
}

void main()
{
printf("%d ", abc());
printf("%d ", abc());

You might also like