0% found this document useful (0 votes)
4 views2 pages

Storage Classes

The document explains the concept of storage classes in C programming, categorizing variables by their data type and storage class. It details four storage classes: Automatic, Static, External, and Register, each with specific characteristics regarding scope and value retention. Key points about each class are highlighted, including their default behaviors and how they interact with each other within a program.

Uploaded by

boylucifer945
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)
4 views2 pages

Storage Classes

The document explains the concept of storage classes in C programming, categorizing variables by their data type and storage class. It details four storage classes: Automatic, Static, External, and Register, each with specific characteristics regarding scope and value retention. Key points about each class are highlighted, including their default behaviors and how they interact with each other within a program.

Uploaded by

boylucifer945
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/ 2

Storage Classes

The variables can be characterized by their data type and by their storage class. One way to
classify a variable is according to its data type and the other can be through its storage class.
Data type refers to the type of value represented by a variable whereas storage class refers
to the permanence of a variable and its scope within the program i.e. portion of the program
over which variable is recognized.
There are four different storage classes in C:
1. Automatic (auto)
2. Static (static)
3. External (extern)
4. Register (register)
Automatic variables
The variables local to a function or a code block are automatic i.e., declared within the
function. The variable exists within the function itself. It is the default storage class for
variables declared in a function. Key points related to automatic variables:
 The keyword auto is optional.
 If not initialized the unpredictable value/arbitrary value will be defined.
 The value is not retained after exit from the code block in which it has been defined.
int x;
Or, auto int x;
Static variables
These variables have the same scope as automatic variables but static variables retain their
values throughout the execution of program if control re-enters in the code block where they
have been defined. Key points related to static variables:
 The specifier static precedes the declaration and the value cannot be accessed outside
of their defining function.
 The static variables may have same name as that of external variables but the local
variables take precedence in the function. Therefore, external variables maintain their
independence with locally defined auto and static variables.
 Zeros are assigned to all variables whose declarations do not include explicit initial
values. Hence they always have assigned values and the Initialization is done only once
(at the time of first execution).
static int x;

External Variables (Global)


These are not confined to a single function. Their scope ranges from the point of declaration
to the entire remaining program. Therefore, their scope may be the entire program or two or
more functions depending upon where they have been declared. Key points related to
external variables:
 These are global and can be accessed by any function within its scope. Therefore, value
may be assigned in one and can be written in another.
 There is difference in external variable definition and declaration.
 The extern specifier is not required in external variable definition however; a
declaration is required with extern keyword if the external variable definition comes
after the function definition.
 If initial value is not included, then it is automatically assigned a value of zero.
Register variables
Registers are special storage areas within a computer’s CPU. All the arithmetic and logical
operations are carried out with these registers. For the same program, the execution time can
be reduced if certain values can be stored in registers rather than memory. These programs
are smaller in size (as few instructions are required) and few data transfers are required. The
reduction is there in machine code and not in source code. Key points related to register
variables:
 These variables are stored in registers of computers. If the registers are not available,
they are put in memory. If the register is not available, the variable is like the
automatic variable.
 Scope is same as automatic variable, local to a function in which they are declared.
 Address of operator ‘&’ cannot be applied to a register variable.
 These variables can be used for loop indices also to increase efficiency.
 These variables are declared using register keyword.

You might also like