Storage class defines the scope (visibility) and lifetime of variables and/or functions declared within a C program. In addition to this, the storage class gives the following information about the variable or the function.
STORAGE
CLASSES
Storage
class defines the scope (visibility)
and lifetime of variables and/or functions declared within a C program. In
addition to this, the storage class gives the following information about the
variable or the function.
•
The storage class of a function or a variable determines the part of memory
where storage space will be allocated for that variable or function (whether
the variable/ function will be stored in a register or in RAM).
•
It specifies how long the storage allocation will continue to exist for that
function or variable.
•
It specifies the scope of the variable or function, i.e., the storage class
indicates the part of the C program in which the variable name is visible or
the part in which it is accessible. In other words, whether the variable/
function can be referenced throughout the program or only within the function,
block, or source file where it has been defined.
•
It specifies whether the variable or function has internal, external, or no
linkage.
•
It specifies whether the variable will be automatically initialized to zero or
to any indeterminate value.
C
supports four storage classes: automatic,
register, external, and static.
The general syntax for specifying the storage class of a variable can be given as:
<storage_class_specifier>
<data type> <variable name>
The
auto storage class specifier is used to explicitly declare a variable with automatic storage. It is the default
storage class for variables declared inside a block. For example, if we write
auto int x;
then
x is an integer that has automatic storage. It is deleted when the block in
which x is declared is exited.
The
auto storage class can be used to
declare variables in a block or the names of function parameters. However,
since the variable names or names of function parameters by default have
automatic storage, the auto storage class specifier is therefore treated as
redundant while declaring data.
Important
things to remember about variables declared with auto storage class include:
•
All local variables declared within a function belong to automatic storage
class by default.
•
They should be declared at the start of the program block, right after the
opening curly bracket {.
•
Memory for the variable is automatically allocated upon entry to a block and
freed automatically upon exit from that block.
•
The scope of the variable is local to the block in which it is declared. These
variables may be declared within a nested block.
•
Every time the block (in which the automatic variable is declared) is entered,
the variable is initialized with the values declared.
•
The auto variables are stored in the
primary memory of the computer.
•
If auto variables are not initialized
at the time of declaration, then they contain some garbage value.
The
following code uses an auto integer
that is local to the function in which it is defined.
#include <stdio.h>
void func1 ()
{
a=10;
printf("\n a = %d", a);
// auto integer local to func1 ()
}
void func2 ()
{
int a=20;
printf("\n a = %d", a);
// auto integer local to func2 ()
}
void main()
{
int a=30; /
/ auto integer local to main()
func1();
func2();
printf("\n a = %d", a);
}
Output
a = 10
a = 20
a = 30
When
a variable is declared using register as its storage class, it is stored in a
CPU register instead of RAM. Since the variable is stored in a register, the
maximum size of the variable is equal to the register size. One drawback of
using a register variable is that they cannot be operated using the unary
'&' operator because it does not have a memory location associated with it.
A register variable is declared in the following manner:
register int x;
Register
variables are used when quick access to the variable is needed. It is not
always necessary that the register variable will be stored in the register.
Rather, the register variable might be stored in a register depending on the
hardware and implementation restrictions.
Hence,
programmers can only suggest to the compiler to store those variables in the
registers which are used repeatedly or whose access times are critical.
However, for the compiler, it is not an obligation to always accept such
requests. In case the compiler rejects the request to store the variable in the
register, the variable is treated as having the storage class specifier auto.
Like
auto variables, register variables also have automatic storage duration. That is,
each time a block is entered, the register
variables defined in that block are accessible and the moment that block is
exited, the variables become no longer accessible for use. Now let us have a
look at the following code that uses a register variable.
#include <stdio.h>
int exp(int a, int b);
int main()
{
int a=3, b=5, res;
res = exp (a, b);
printf("\n %d to the power of
%d = %d", a, b, res);
return 0;
}
int exp (int a, int b)
{
register int res=1;
int i;
for (i=1;i<=b; i++)
res = res*a;
return res;
}
Output
3 to the power of 5 = 243
A
large C program can be broken down into smaller programs. When these smaller
programs are compiled, they are joined together to form a large program.
However, these smaller programs may need to share certain variables for
processing. For such situations C language provides an external storage class
that is specified using the keyword extern.
The
extern storage class is used to give
a reference of a global variable that is visible to all the program files. Such
global variables are declared like any other variable in one of the program
files. When there are multiple files in a program and you need to use a
particular function or variable in a file apart from which it is declared, then
use the extern keyword. To declare a
variable x as extern write,
extern int x;
External
variables may be declared outside any function in a source code file as any
other variable is declared. But usually external variables are declared and
defined at the beginning of a source file.
Memory
is allocated for external variables when the program begins execution, and
remains allocated until the program terminates. External variables may be
initialized while they are declared. However, the initializer must be a
constant expression. The compiler will initialize its value only once during
the compiler time. In case the extern variable is not initialized, it will be
initialized to zero by default.
External
variables have global scope, i.e., these variables are visible and accessible
from all the functions in the program. However, if any function has a local
variable with the same name and type as that of the global or extern variable,
then references to the name will access the local variable rather than the extern variable. Hence extern variables
are overwritten by local variables. Let us now write a program in which we will
use the extern keyword.
// FILE 1.C
#include <stdio.h>
#include <FILE2.C>
// Programmer's own header file
int x;
void print (void);
int main()
{
x = 10;
printf("\n x in FILE1 =
%d", x);
print();
return 0;
}
// END OF FILE1.C
// FILE2.C
#include <stdio.h>
extern int x;
void print ()
{
printf("\n x in FILE 2 =
%d", x);
}
main()
{
// Statements
}
// END OF FILE2.C
Output
x in FILE1 = 10
x in FILE2 = 10
In
the program, we have used two files-File1
and File2. File1 has declared a global variable x. File1 also includes File2
which has a print function that uses
the external variable x to print its value on the screen.
Note
The
extern specifier tells the compiler that the variable has already been declared
elsewhere and therefore it should not allocate storage space for that variable
again. During compilation of the program, the linker will automatically resolve
the reference problem.
In
a multi-file program, the global variable must be declared only once (in any
one of the files) without using the extern
keyword. This is because otherwise the linker will have a conflict as to which
variable to use and therefore in such a situation it raises a warning message.
While
auto is the default storage class for
all local variables, static is the
default storage class for all global variables. Static variables have a
lifetime over the entire program, i.e., memory for the static variables is allocated when the program begins running and
is freed when the program terminates. To declare an integer x as static, write
static
int x = 10;
Here
x is a local static variable. Static local variables when defined within a
function are initialized at the runtime. The difference between an auto variable and a static variable is that the static
variable when defined within a function is not re-initialized when the function
is called again and again. It is initialized just once and further calls of the
function share the value of the static
variable. Hence, the static variable
inside a function retains its value during various calls.
When
a static variable is not explicitly
initialized by the programmer, then it is automatically initialized to zero
when memory is allocated for it. Although static
automatic variables exist even after the block in which they are defined
terminates, their scope is local to the block in which they are defined.
Static
storage class can be specified for auto as well as extern variables. For
example we can write,
static extern int x;
When
we declare a variable as extern static
variable, then that variable is accessible from all the functions in this
source file.
Note
static
variables can be initialized while they are being declared. But this
initialization is done only once at the compile time when memory is being
allocated for the static variable. Also the value with which the static
variable is initialized must be a constant expression.
Look
at the following code which clearly differentiates between a static variable and a normal variable.
#include <stdio.h>
void print (void);
int main ()
{
printf("\n First call of print
()");
print();
printf("\n\n Second call of
print ()");
print();
printf("\n\n Third call of
print () ");
print();
return 0;
}
void print()
{
static int x;
int y = 0;
printf("\n Static integer
variable, x = %d", x);
printf("\n Integer variable, y
= %d", y);
x++;
y++;
}
Output
First call of print()
Static integer variable, x = 0
Integer variable, y = 0
Second call of print ()
Static integer variable, x = 1
Integer variable, y = 0
Third call of print ()
Static integer variable, x = 2
Integer variable, y = 0
Programming in C: Unit III (a): Functions : Tag: : Syntax with Example C Programs - Storage Classes
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation