C Language / Lesson 6 | Back to Index or to Previous Page or to Next Page |
Storage Classes
General
The storage class of a variable (or function) name describes the visibility (scope) of the name and the run-time life cycle of this name. In a program module the declared names have a scope within this module but also they may or may not be available to other modules (other functions written in different modules or libraries used by the program).
Within a module : Two rules that are never violated
I.
A name declared in a module is known only after its declaration line
|
II.
A name declared inside a function body is only known within that function
body
|
a. External variables
An external name is a name available to all modules (other languages use the words global or public for this).
However, there is a confusion in declarations of external variables in C because a variable available to other modules, in addition to its definition (which can only be done once) , must be declared in every module where it will be used, because otherwise the complilation of that module will generate a 'Not declared' error.
Definition of an external variable |
All variable declarations done outside function bodies and not beginning with a storage class declarator are (by default) external definitions |
|
Declaration of an external variable |
A variable declaration beginning with the storage class declarator 'extern' declares external variables example : declarations merely announce that 'somewhere else the name has been defined' This 'somewhere' may even be in the very same module. This is really useful when declarations are inside header files, which can be included in the mopdulse which contains the definition of the name |
b. Auto variables
Auto variables can only be local to a function and the 'auto' storage class is the default class inside function bodies i.e. auto variables are declared inside function bodies An auto variable is temporary: it is created (automatically) when the function is called and 'dies' when the function terminates.
example:
int |
|
{ |
int n; // auto variable |
} |
A special sub class of auto
Register variables
An auto variable is local and temporary and it is a practice to most languages to store this type of variables in ths Stack. Of course the Stack is a memory area and as all other variables in memory have to travel back and forth whenever they are used in expressions or receive new values. Stack addressing is a little worse in speed than other variables.
That's why a brilliant idea called Register variables was invented : One or
two auto variables in every function can be stored in registers in the
CPU thus avoiding the transfers over the Data bus. In other words register variables
are auto variables with a significant privilege : they are faster.
BUT :
A register variable does not have an address |
A register variable must be a scalar (because an aggregate can't fit in the CPU) |
A register variable (as auto) cannot be an external or static variable |
c. Static variables
Static names (variables and functions) are for some authors "the opposite of external" that is not available outside the module in which they are defined. For some others they are "the opposite of auto" that is permanent (they continuously exist during run-time).
This is not confusing because in fact static variables are both of the above. Let's put it in this way : Static variables are permanent variables not available to other modules. They can be declared outside or inside function bodies. But thus a static variable inside a module retains its value from call to call and this makes it an alternative to auto variables which are temporary and lose their value from call to call. On the other hand a static variable defined outside a function body would be permanent anyway (even if it were not static) and so what really changes is that the variable is not available to other modules.
Back to top or to Previous Page or to Next Page