0% found this document useful (0 votes)
14 views5 pages

Storage Class

A storage class in C programming defines the visibility, location, initialization, lifetime, and accessibility of a variable. There are four standard storage classes: auto (default local variable), extern (global variable), static (local or global variable with a lifetime throughout the program), and register (local variable stored in CPU registers for quick access). Each storage class has specific characteristics regarding scope and lifetime, influencing how variables are managed in the code.

Uploaded by

sarthak1058
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)
14 views5 pages

Storage Class

A storage class in C programming defines the visibility, location, initialization, lifetime, and accessibility of a variable. There are four standard storage classes: auto (default local variable), extern (global variable), static (local or global variable with a lifetime throughout the program), and register (local variable stored in CPU registers for quick access). Each storage class has specific characteristics regarding scope and lifetime, influencing how variables are managed in the code.

Uploaded by

sarthak1058
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/ 5

What is a Storage Class?

A storage class represents the visibility and a location of a variable. It tells from what
part of code we can access a variable. A storage class is used to describe the following
things:

 The variable scope.


 The location where the variable will be stored.
 The initialized value of a variable.
 A lifetime of a variable.
 Who can access a variable?

Thus a storage class is used to represent the information about a variable.

NOTE: A variable is not only associated with a data type, its value but also a storage
class.

There are total four types of standard storage classes. The table below represents the
storage classes in 'C'.

Storage class Purpose

auto It is a default storage class.

extern It is a global variable.

static It is a local variable which is capable of returning


a value even when control is transferred to the
function call.

register It is a variable which is stored inside a Register.

The next table summarizes the principal features of each storage


class which are commonly used in C programming
Storage Declaration Storage Default Initial Scope Lifetime
Class Value

auto Inside a Memory Unpredictable Within the function/block Within the


function/block function/block

register Inside a CPU Garbage Within the function/block Within the


function/block Registers function/block

extern Outside all Memory Zero Entire the file and other program
functions files where the variable runtime
is declared as extern

Static Inside a Memory Zero Within the function/block program


(local) function/block runtime

Static Outside all Memory Zero Global program


(global) functions runtime

The auto Storage Class

The variables defined using auto storage class are called as local variables. Auto
stands for automatic storage class. A variable is in auto storage class by default if it is
not explicitly specified.

The scope of an auto variable is limited with the particular block only. Once the control
goes out of the block, the access is destroyed. This means only the block in which the
auto variable is declared can access it.

A keyword auto is used to define an auto storage class. By default, an auto variable
contains a garbage value.
The auto storage class is the default storage class for all local variables.
{
int mount;
auto int month;
}

The example above defines two variables with in the same storage class. 'auto' can
only be used within functions, i.e., local variables.

The register Storage Class

The register storage class is used to define local variables that should be stored in a
register instead of RAM. This means that the variable has a maximum size equal to the
register size (usually one word) and can't have the unary '&' operator applied to it (as it
does not have a memory location).
{
register int miles;
}
The register should only be used for variables that require quick access such as
counters. It should also be noted that defining 'register' does not mean that the variable
will be stored in a register. It means that it MIGHT be stored in a register depending on
hardware and implementation restrictions.

Extern storage class

Extern stands for external storage class. Extern storage class is used when we have
global functions or variables which are shared between two or more files.

Keyword extern is used to declaring a global variable or function in another file to


provide the reference of variable or function which have been already defined in the
original file.

The variables defined using an extern keyword are called as global variables. These
variables are accessible throughout the program. Notice that the extern variable cannot
be initialized it has already been defined in the original file

Example, extern void display();

First File: main.c


#include <stdio.h>

extern i;

main() {
printf("value of the external integer is = %d\n", i);

return 0;}

Second File: original.c


#include <stdio.h>

i=48;

Result:

value of the external integer is = 48

The static Storage Class

The static storage class instructs the compiler to keep a local variable in existence
during the life-time of the program instead of creating and destroying it each time it
comes into and goes out of scope. Therefore, making local variables static allows them
to maintain their values between function calls.
The static modifier may also be applied to global variables. When this is done, it
causes that variable's scope to be restricted to the file in which it is declared.
In C programming, when static is used on a global variable, it causes only one copy of
that member to be shared by all the objects of its class.

#include <stdio.h>

/* function declaration */
void func(void);

static int count = 5; /* global variable */

main() {

while(count--) {
func();
}

return 0;
}
/* function definition */
void func( void ) {

static int i = 5; /* local static variable */


i++;

printf("i is %d and count is %d\n", i, count);


}
When the above code is compiled and executed, it produces the following result −
i is 6 and count is 4
i is 7 and count is 3
i is 8 and count is 2
i is 9 and count is 1
i is 10 and count is 0

You might also like