There are two ways to characterize variables: by data type, and by storage class.
There are four storage class specifiers supported by C:
auto
static
register
extern
auto
features
Storage main memory
Default Initial value garbage
Scope local to the block in which it is defined
Life as long control within the block in which it is defined
Static
Static variables are permanent variables within their own function or file. Unlike global variables, they are not known outside their function or file, but they maintain their values between calls. This feature makes it useful.
features
Storage main memory
Default initial value 0
Scope local to the block in which it is defined
Life retains its value between function calls (or files)
static local variables
Static global variable
Applying the specifier static to a global variable instructs the compiler to create a global variable that is known only to the file in which it is declared. This means that even though the variable is global, routines in other files may have no knowledge of it or alter its contents directly.
The key difference between a static local variable and a global variable is that a static local variable is a local variable that retains its value between function calls.
register
features
Storage Registers of CPU
Default initial value Garbage
Scope Local to the block in which to defined
Life As long as the control remains within the block in which it is defined
extern
Because C allows separately compiled modules of a large program to be linked to speed up compilation and help manage large projects. There must be some way telling all the files about the global variables required by the program. A global variable can be declared once only. If a global variables will be declared more than once in the same name, then there will be an error message. The same problem will occur, if all the global variables needed by the program will be declared in each file. The solution is to declare all global variables in the file and use extern declaration in other.
features
Storage Main memory
Default O
Scope Global
Life As long as program is an execution
extern variable declarations can occur inside the same file as the global declaration, but they are not necessary.
void main ()
{
extern int x;
. . . . .
. . . . .
. . . . .
}
About the Author
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc