Difference in C language Term
Categories: C language
Differences b/w static and global variable
Global variables are the variables that are declared outside the function. These global variables exist at the beginning of the program, and its scope remains till the end of the program. It can be accessed outside the program also.
Static variables are limited to the source file in which they are defined, i.e., they are not accessible by the other source files.
Both the static and global variables have static initialization. Here, static initialization means if we do not assign any value to the variable then by default, 0 value will be assigned to the variable.
Differences b/w static local and static global variable
Static global variable
If the variable declared with a static keyword outside the function, then it is known as a static global variable. It is accessible throughout the program.
Static local variable
The variable with a static keyword is declared inside a function is known as a static local variable. The scope of the static local variable will be the same as the automatic local variables, but its memory will be available throughout the program execution. When the function modifies the value of the static local variable during one function call, then it will remain the same even during the next function call.
Properties of a static variable
1. The memory of a static variable is allocated within a static variable.
2. Its memory is available throughout the program, but the scope will remain the same as the automatic local variables. Its value will persist across the various function calls.
3. If we do not assign any value to the variable, then the default value will be 0.
4. A global static variable cannot be accessed outside the program, while a global variable can be accessed by other source files.