Storage class specifier declares that a variable or function declred in a program where stored.There are following types of storage specifier in C :-
1)auto
2)register
3)extern
4)static
5)typedef
6)__declspec
7)mutable
8)thread_local
(1)auto:-auto is a keyword used to declare an automatic storage class.It is declared as following types :-
auto a=2; //auto
auto b=3.8; //float
auto c='h'; //char
auto b1=3.8f; //float
auto b2=3.8F; //float
auto b3=3.8l; //double
auto b4=3.8L; //long double
printf("a=%d\n",a); //int
printf("b=%f\n",b); // float
printf("c =%c\n",c); //char
2)resister:-resister is also a storage class used to faster access of variable because the storage will be done in CPU registers .It is a hint to the compiler that the variable will be heavily used and that recommend it will be kept in a processor register if possible.We can declare a datatype register in following manner:-
register int ri=98;
3)extern:-extern is also a storage class specifier .It is used for the use a variable which is initialized in another file in same project.When we use extern modifier with any variables it is only declaration i.e. memory is not allocated for these variable.We can declare register keyword in following manner:-
extern int ei=23;
4)Static:-static is also a storage class specifier used in C/C++.It also stores variable in memory.The default initial value is zero.The value of the variable persists between different function calls.We can use a static datatype in following manner:-
#include <stdio.h>
void func() {
static int x = 0; // x is initialized only once across three calls of func()
printf("%d\n", x); // outputs the value of x
x = x + 1;
}
int main(int argc, char * const argv[]) {
func(); // prints 0
func(); // prints 1
func(); // prints 2
return 0;
}
#include <stdio.h>
void func() {
static int x = 0; // x is initialized only once across three calls of func()
printf("%d\n", x); // outputs the value of x
x = x + 1;
}
int main(int argc, char * const argv[]) {
func(); // prints 0
func(); // prints 1
func(); // prints 2
return 0;
}
(5)typedef:-typedef keyword provide us to rename a variable datatype into a short and meaningful way.
Ex:-
typedef unsigned long int ULI;
ULI a;
13)_declspec:-It is a storage class modifier used with many extended attribute like dllimport and dllexport.
7)mutable:-This keyword can only be applied to non-static and non-const data members of a class. If a data member is declared mutable, then it is legal to assign a value to this data member from a const member function.
mutable member-variable-declaration;
8)thread_local:-The variable is allocated when the thread begins and deallocated when the thread ends. Each thread has its own instance of the variable. Only variables declared thread_local have this storage duration. thread_local can only be declared for global variables, plus those declared with static or extern.It is declared as following:-
thread_local unsigned int rage = 1
Here are some inportant notes about storage class specifiers:-
C program instructions get stored in code/text segment
Register variables are stored in Register.
The memory created dynamically are stored in Heap.
Local Variables/arrays (Except static) are stored in Stack.
Global, extern & static variables/arrays are stored in data segment.
a) Uninitialized static/global/extern variables are stored in the BSS(Block started by symbol) of the data segment .ie high address..
(Note: Uninitialised static variable is automatically initialised to zero)
(Note: Uninitialised global variable is automatically initialised to zero)
(Note: Uninitialised extern variable is automatically initialised to zero)
b) Initialized static/global/extern variables are stored in data section of the data segment. ie low address
(Initialised static also includes zero initialised static variable)
const local variable(initialised) is stored on stack.
const local var (uninitialized) is NOT POSSIBLE. It must be initialized when declared.
const global var (initialized) is stored in data section of data segment.
const global var (uninitialized) is NOT POSSIBLE. It must be initialized when declared.
-----------
null pointer -> .bss
static pointer (uninitialised)-> (initialized to null by default) -> null pointer -> .bss
command line arguments (stack)
function parameters (stack)
----------
Data in the bss segment is initialized by the kernel to zero before the program starts executing.
------------
Default storage class specifiers:
For external declarations (outside a function) the default storage class specifier will be extern and for internal declarations(inside a function) it will be auto.
default storage class specifier for functions is always extern.
We can understand this more easily by following details:-
Object | Storage Class Specifiers | Storage Duration (Life Time) | Linkage | Scope (Accesibility/Visibility) | Storage Area | |||||
Internal variable | auto | Automatic | No | Function/Block/Compound Stmt | SS:stack | Initialized with garbage value | ||||
Internal variable | register | Automatic | No | Function/Block/Compound Stmt | Register | |||||
Internal variable | static | static | No | Function/Block/Compound Stmt | DS | Uninitialized | DS:BSS | Initialized with zero | ||
Internal variable | extern | static (???) | external (???) | Function/Block/Compound Stmt | None | Initialized | DS:data | |||
Internal variable | none (default auto) | Automatic | No | Function/Block/Compound Stmt | SS:stack | Initialized with garbage value | ||||
Internal Function | auto | static (???) | external (???) | Project (multiple files) | CS | |||||
Internal Function | register | invalid | invalid | invalid | invalid | |||||
Internal Function | static | invalid | invalid | invalid | invalid | |||||
Internal Function | extern | static (???) | external (???) | Project (multiple files) | CS | |||||
Internal Function | none (default extern) | static (???) | external (???) | Project (multiple files) | CS | |||||
External variable | auto | static | external | Project (multiple files) | DS | |||||
External variable | register | invalid | invalid | invalid | invalid | |||||
External variable | static | static | internal | File | DS | Uninitialized | DS:BSS | Initialized with zero | ||
External variable | extern | static | external | Project (multiple files) | DS/None | Initialized | DS:data | |||
External variable | none (default extern) | static | external | Project (multiple files) | DS | |||||
External Function | auto | static | external | Project (multiple files) | CS | |||||
External Function | register | invalid | invalid | invalid | invalid | |||||
External Function | static | static | internal | File | CS | Storage Area | ||||
External Function | extern | static | external | Project (multiple files) | CS | extern int i; | None | intialized to 5 | ||
External Function | none (default extern) | static | external | Project (multiple files) | CS | int i=5; | ||||
thread_local | thread | No | extern int i=6; | DS:data | intialized to 6 | |||||
dynamic mem allocation | Dynamic | No | SS:Heap | |||||||
extern int i; | None | intialized to 0 | ||||||||
Automatic | No | int i; | ||||||||
Thread | No | |||||||||
Dynamic | No | |||||||||
No comments:
Post a Comment