In the C language, the variable's scope
refers to the part of the program where we reference it. The two types of variables in C are:
1. Local variables are defined within a function body. They are not accessible outside the function.
2. Global variables are defined outside the function body. They are accessible to the entire program.
The static
specifier is one of the four standard storage classes in C.
A global static variable is declared before the function declarations in a C program using the static
keyword, as shown below:
static int a = 10;
The four standard storage classes in C are
static, register, auto, and extern
.
The global static
variable is accessible by all the functions in the same source file as the variable. This variable has a File Scope
, i.e., we can access the variable anywhere in the same C file. Therefore, the variables with File Scope
do not conflict with other variables because they are private to the source file.
The code below shows the static
global variable a. Since a has a File Scope
, it is accessible both within and outside the function. It is referenced in lines 7 and 13, as shown below:
#include <stdio.h>// A Static Global Variablestatic int a = 20;int func() {a = a + 20;printf ("The value of a is: %d\n", a);}int main() {func();a = a + 5;printf ("The value of a is: %d\n", a);return 0;}
Free Resources