Automatic Storage class:
The features of a variable defined to have an automatic storage class are as under:
- Storage : Memory
- Default initial value : unpredictable value which is often called a garbage value.
- Scope : Local to the block in which the variable is defined.
- Life : Till the control remains within the block in which the Variable is defined.
- Keyword : Auto
The variables are declared inside a function block are automatic variables. A variable declared inside a function block without a storage class name, by default is an auto variable.
{ int mount; auto int month; }
Register storage class
The features of a variable defined to be of register storage class are as under:
- Storage : CPU registers.
- Default initial value : unpredictable value which is often called a garbage value.
- Scope : Local to the block in which the variable is defined.
- Life : Till the control remains within the block in which the Variable is defined.
- Keyword : register.
We can also keep some variables in the CPU registers instead of memory. The keyword register tells the compiler that the variable list followed by it is kept on the CPU registers, since register access is faster than the memory access. So, the variables that are declared with storage class register are known as register variables.
{ register int miles; }
Static Storage class
The features of a variable defined to have a static storage class are as under:
- Storage : Memory.
- Default initial value : Zero.
- Scope : Local to the block in which the variable is defined.
- Life : Value of the variable persists between different function Calls.
- Keyword : static.
The static variable may be of an internal or external type, depending upon where it is declared. If declared outside the function of the body it will be static global. In case it is declared in the body or block it will be auto variable. A static variable is initialized only once, it is never reinitialized.
#include <stdio.h> /* function declaration */ void increment(void); main() { increment(); increment(); increment(); } /* function definition */ void increment(void) { static int i = 1; /* local static variable */ printf("%d\n", i); i++; }
The output of the programs would be: 1 2 3
External Storage class
The features of a variable whose storage class has been defined as external as follows:
- Storage : Memory.
- Default initial value : Zero.
- Scope : Global.
- Life : As long as the program’s execution doesn’t comes to end.
- Keyword : extern.
The variables that are available to all the functions i.e. from entire program they can be accessed are known as external or global variables. External variables are declared outside the function body. In case both external and auto variables are declared with the same name, in a program the first priority is given to the auto variable. In this case external variable is hidden.
#include <stdio.h> int count ; extern void write_extern(); main() { count = 5; write_extern(); }
No comments:
Post a Comment