C langugae : Storage Classes

During the execution of the program, each object exists as a location in memory for a certain period, called its lifetime . There is no way to access an object before or after its lifetime. For example, the value of a pointer becomes invalid when the object that it references reaches the end of its lifetime.
In C, the lifetime of an object is determined by its storage duration . C does not specify how objects must actually be stored in any particular system architecture, but typically, objects with static storage duration are located in a data segment of the program in memory, while objects with automatic storage duration are located on the stack. Allocated storage is memory that the program obtains at runtime by calling the malloc( ), calloc( ), and realloc( ) functions.
Scope
A C variable has one of the following scopes: block scope, function prototype scope, or file scope. The program examples to date have used block scope.A block is a region of code contained within an opening brace and the matching closing brace.The entire body of a function is a block. Any compound statement within a function also is a block. A variable defined inside a block has block scope, and it is visible from the point it is defined until the end of the block containing the definition. Also, formal function parameters, even though they occur before the opening brace of a function, have block scope and belong to the block containing the function body. So the local variables we've used to date, including formal function parameters, have block scope.
Linkage
A C variable has one of the following linkages: external linkage, internal linkage, or no linkage. Variables with block scope or prototype scope have no linkage. That means they are private to the block or prototype in which they are defined. A variable with file scope can have either internal or external linkage. A variable with external linkage can be used anywhere in a multifile program. A variable with internal linkage can be used anywhere in a single file.
int giants = 5; // file scope, external linkage
static int dodgers = 3; // file scope, internal linkage
int main()
{
...
}
Features
A variable’s storage class tells us:
(a)Where the variable would be stored.
(b) What will be the initial value of the variable, if initial value is not specifically assigned.(i.e. the default initial value).
(c) What is the scope of the variable; i.e. in which functions the value of the variable would be available.
(d) What is the life of the variable; i.e. how long would the variable exist.
There are four storage classes in C:
(a) Automatic storage class
(b) Register storage class
(c) Static storage class
(d) External storage class

Storage Classes

Storage Class Duration Scope

Linkage

How Declared
automatic

Automatic

Block

None

In a block

register

Automatic

Block

None

In a block with the keyword register

static with external linkage

Static

File

External

Outside of all functions

static with internal linkage

Static

File

Internal

Outside of all functions with the keyword static

static with no linkage

Static

Block

None

In a block with the keyword static


Automatic Storage Class
The terms automatic and static describe what happens to local variables when a function returns to the calling procedure. By default, all local variables are automatic, meaning that they are erased when their function ends. To declare a variable as an automatic variable, prefix its definition with the term auto. The auto keyword is optional with local variables because they are automatic by default.
The two statements after main()'s opening brace declare automatic local variables:
main()
{
int i;
auto float x;
/* Rest of main() goes here */
Because auto is the default, we did not need to include the term auto with x.
main( )
{
auto int i = 1 ;
{
auto int i = 2 ;
{
auto int i = 3 ;
printf ( "\n%d ", i ) ;
}
printf ( "%d ", i ) ;
}
printf ( "%d", i ) ;
}
The output of the above program would be: 3 2 1
Static storage class
Objects that are defined outside all functions, or within a function and with the storage class specifier static, have static storage duration . These include all objects whose identifiers have internal or external linkage.
All objects with static storage duration are generated and initialized before execution of the program begins. Their lifetime spans the program's entire runtime.

Automatic and Static Storage Classes

main( )
{
increment( ) ;
increment( ) ;
increment( ) ;
}
increment( )
{
auto int i = 1 ;
printf("%d\n", i ) ;
 i = i + 1 ;
}
main( )
{
increment( ) ;
increment( ) ;
increment( ) ;
}
increment( )
{
static int i = 1 ;
printf( "%d\n", i ) ;
 i = i + 1 ;
}
The output of the above programs would be:
1
1
1
1
2
3

In the above example, when variable i is auto, each time increment( ) is called it is re-initialized to one. When the function terminates, i vanishes and its new value of 2 is lost. The result: no matter how many times we call increment( ), i is initialized to 1 every time.
On the other hand, if i is static, it is initialized to 1 only once. It is never initialized again. During the first call to increment( ), i is incremented to 2. Because i is static, this value persists. The next time increment( ) is called, i is not re-initialized to 1; on the contrary its old value 2 is still available. This current value of i (i.e. 2) gets printed and then i = i + 1 adds 1 to i to get a value of 3. When increment( ) is called the third time, the current value of i (i.e. 3) gets printed and once again i is incremented. In short, if the storage class is static then the statement static int i = 1 is executed only once, irrespective of how many times the same function is called.
Register storage class
Variables are normally stored in computer memory. Register variables are stored in the CPU registers or, more generally, in the fastest memory available, where they can be accessed and manipulated more rapidly than regular variables. Because a register variable may be in a register rather than in memory, you can't take the address of a register variable. In most other respects, register variables are the same as automatic variables. That is, they have block scope, no linkage, and automatic storage duration. A variable is declared by using the storage class specifier register:
main( )
{
register int i ;
for ( i = 1 ; i <= 10 ; i++ )
printf ( "\n%d", i ) ;
}
Not every type of variable can be stored in a CPU register
The number of CPU registers are limited, and they may be busy doing some other task.For example, if the microprocessor has 16-bit registers then they cannot hold a float value or a double value, which require 4 and 8 bytes respectively. However, if you use the register storage class for a float or a double variable you won’t get any error messages. All that would happen is the compiler would treat the variables to be of auto storage class.
External storage class
C defines three categories of linkage: external, internal, and none. In general, functions and global variables have external linkage. This means they are available to all files that constitute a program. File scope objects declared as static have internal linkage. These are known only within the file in which they are declared. Local variables have no linkage and are therefore known only within their own block.
External variables are declared outside all functions, yet are available to all functions that care to use them. Here is an example to illustrate this fact.
Initializing External Variables
Like automatic variables, external variables can be initialized explicitly. Unlike automatic variables, external variables are automatically initialized to zero if you don't initialize them.
int x = 10; // ok, 10 is constant
int y = 3 + 20; // ok, a constant expression
size_t z = sizeof(int); // ok, a constant expression
int x2 = 2 * x; // not ok, x is a variable
Example :- This experiments contain two c programs name a.c and b.c. First create these program and excute the program b.c
// part a.c --- various storage classes
#include < stdio.h >
void report_count();
void accumulate(int k);
int count = 0; // file scope, external linkage
int main(void)
{
int value; // automatic variable
register int i; // register variable
printf("Enter a positive integer (0 to quit): ");
while (scanf("%d", &value) == 1 && value > 0)
{
++count; // use file scope variable
for (i = value; i >= 0; i--)
accumulate(i);
printf("Enter a positive integer (0 to quit): ");
}
report_count();
return 0;
}
void report_count()
{
printf("Loop executed %d times\n", count);
}
// partb.c -- rest of the program
#include < stdio.h >
#include "a.c"
extern int count; // reference declaration, external linkage
static int total = 0; // static definition, internal linkage
void accumulate(int k); // prototype
void accumulate(int k) // k has block scope, no linkage
{
static int subtotal = 0; // static, no linkage
if (k <= 0)
{
printf("loop cycle: %d\n", count);
printf("subtotal: %d; total: %d\n", subtotal, total);
subtotal = 0;
}
else
{
subtotal += k;
total += k;
}
}
Here's a sample run:
Enter a positive integer (0 to quit): 5
loop cycle: 1
subtotal: 15; total: 15
Enter a positive integer (0 to quit): 10
loop cycle: 2
subtotal: 55; total: 70
Enter a positive integer (0 to quit): 2
loop cycle: 3
subtotal: 3; total: 73
Enter a positive integer (0 to quit): 0
Loop executed 3 times
Storage Class Short Summary
Automatic variables have block scope, no linking, and automatic storage duration. They are local and private to the block (typically a function) where they are defined. Register variables have the same properties as automatic variables, but the compiler may use faster memory or a register to store them. You can't take the address of a register variable.
Variables with static storage duration can have external linkage, internal linkage, or no linkage. When a variable is declared external to any function in a file, it's an external variable and has file scope, external linkage, and static storage duration. If you add the keyword static to such a declaration, you get a variable with static storage duration, file scope, and internal linkage. If you declare a variable inside a function and use the keyword static, the variable has static storage duration, block scope, and no linkage.
Memory for a variable with automatic storage duration is allocated when program execution enters the block containing the variable declaration and is freed when the block is exited. If uninitialized, such a variable has a garbage value. Memory for a variable with static storage duration is allocated at compile time and lasts as long as the program runs. If uninitialized, such a variable is set to 0.
A variable with block scope is local to the block containing the declaration. A variable with file scope is known to all functions in a file following its declaration. If a file scope variable has external linkage, it can be used by other files in the program. If a file scope variable has internal linkage, it can be used just within the file in which it is declared.


Free Web Hosting