Internal static variable vs. External static variable with Examples in C
The static variable may be internal or external depending on the place of declaration. Static variables are stored in initialised data segments.
Internal Static Variables: Internal Static variables are defined as those having static variables which are declared inside a function and extends up to the end of the particular function.
Syntax:
main( )
{
static datatype variable;
// other statements
}
Example:
// C program to demonstrate // Internal Static Variables #include <stdio.h> int value(); int main() { printf("%d", value()); return 0; } int value() { static int a = 5; return a; } |
5
External Static Variables: External Static variables are those which are declared outside a function and set globally for the entire file/program.
Syntax:
static datatype variable;
main()
{
statements
}
function1()
{
statements
}
Example:
// C program to demonstrate // External Static Variables #include <stdio.h> int add(int, int); static int a = 5; int main() { int c; printf("%d", add(a, c)); } int add(int c, int b) { b = 5; c = a + b; return c; } |
10
Difference between internal static variables and External static variables:
| Parameter | Internal Static Variables | External Static Variables |
|---|---|---|
| Keyword | ”static” | ”static” |
| Linkage | Internal static variable has no linkage. | External static variables has internal linkage. |
| Declaration | Internal static variables are declared within the main function | External static variables are declared above the main function. |
| Comparison | Internal static variables are similar to auto(local) variables. | External static variables are similar to global(external) variables. |
| Visibility | Internal static variables are active(visibility) in the particular function. | External Static variables are active(visibility)throughout the entire program. |
| Lifetime | Internal static variables are alive(lifetime) until the end of the function. | External static variables are alive(lifetime) in the entire program. |
| Scope | Internal static variables has persistent storage with block scope(works only within a particular block or function). | External static variables has permanent storage with file scope(works throughout the program). |
Recommended Posts:
- Understanding "static" in "public static void main" in Java
- Difference between static and non-static variables in Java
- Difference between static and non-static method in Java
- Can we access global variable if there is a local variable with same name?
- Initialize a static map in Java with Examples
- Static Variables in Java with Examples
- Difference between Internal and External fragmentation
- Static functions in C
- Static Variables in C
- Static Keyword in C++
- Internal Linkage and External Linkage in C
- C++ | Static Keyword | Question 1
- C++ | Static Keyword | Question 2
- Static import in Java
- Initialize a static Map using Java 9 Map.of()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

