Storage Classes Overview
What is a Storage Class in C
A storage class in C is used to specifies the lifetime, scope, storage, and visibility of a variable. It is a keyword or qualifier that can be applied to a variable or function to specify its properties.
There are four main storage classes in C:
auto: The default storage class for variables declared within a function. The variable is created and initialized when the function is called and destroyed when the function exits.
static: The variable retains its value between function calls and is only created and initialized once.
extern: The variable is defined in another source file and is made available to the current source file.
register: The variable is stored in a CPU register instead of memory, which may result in faster access times, but there are less registers than memory, so this storage class should be used with care.
Storage Duration (Lifetime)
Storage duration (also called the Lifetime of a variable) is a term that refers to the lifetime of a variable, or how long the memory allocated for a variable remains valid.
The storage duration of a variable is determined by its storage class.
Auto and Static storage classes define the storage duration of a variable within a C program.
"auto" storage class
- Variables declared with the "auto" storage class or without any storage class specifier have automatic storage duration.
- These variables are created when the block or function in which they are declared is entered and are destroyed when the block or function is exited. Their lifetime is determined by the scope in which they are defined.
"static" storage class
- Variables declared with the "static" storage class have static storage duration.
- These variables are created once when the program starts and are destroyed when the program ends.
- Their lifetime is determined by the program execution and not by the scope in which they are defined.
There are other storage classes like "register" and "extern" but they do not affect the storage duration of the variable.
Variable Scope (Visibility)
Variable scope, also known as visibility, refers to the regions of a program in which a variable can be accessed.
In C, a variable's visibility is determined by its storage class and its location of declaration.
A variable with global scope, declared outside of any function or block, can be accessed from anywhere in the program.
A variable with local scope, declared within a function or block, can only be accessed within the function or block in which it is declared.
A variable with file scope, declared outside of any function or block but within a source file, can be accessed from anywhere within the source file but not from other source files.
"static" and "extern" storage class affect the visibility of a variable.
Storage Class | Default Value | Scope | Lifetime |
---|---|---|---|
auto | Undefined | Local | Function call and return |
static | 0 (for numeric variables) or NULL (for pointers) | Local/Global | Program execution |
extern | Depends on the declaration | Global | Program execution |
register | Undefined | Local | Function call and return |
It's important to note that the default value of a variable is the value that the variable takes when it is declared without an explicit initial value.
Also, "register" storage class is not recommended for use as it is up to the compiler to decide whether to allocate the variable in a register or memory.
"extern" variables are created when the program starts and are destroyed when the program ends, they are used to share the variables across the multiple files.