# Local, Global and Static variables in C

Last updated on July 27, 2020

## Local Variables #

The variables which are declared inside the function, compound statement (or block) are called Local variables.

 1 2 3 4 5 6 7 8 9 void function_1() { int a, b; // you can use a and b within braces only } void function_2() { printf("%d\n", a); // ERROR, function_2() doesn't know any variable a } 

a and b are called local variables. They are available only inside the function in which they are defined (in this case function_1()). If you try to use these variables outside the function in which they are defined, you will get an error. Another important point is that variables a and b only exists until function_1() is executing. As soon as function function_1() ends variables a and b are destroyed.

Consider the following code:

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include int main() { int a = 100; { /* variable a declared in this block is completely different from variable declared outside. */ int a = 10; printf("Inner a = %d\n", a); } printf("Outer a = %d\n", a); // signal to operating system everything works fine return 0; } 

Expected Output:

 1 2 3 Inner a = 10 Outer a = 100 d 

The variable a created inside the compound statement or block i.e inside braces ({}) is completely different from variable a declared outside the block. As soon as the block ends the variable a declared inside the block is destroyed.

You can use the same variable names in a different function and they will not conflict with each other. For example:

 1 2 3 4 5 6 7 8 9 void function_1() { int a = 1, b = 2; } void function_2() { int a = 10, b = 20; } 

In this case variables a and b inside function_1() are local to function_1(), while variables a and b inside function_2() are local to function_2(). They are entirely independent of each other. If you change the value of a inside the function_1() then it will not change the value of a inside the function_2().

## Global Variables #

The variables declared outside any function are called global variables. They are not limited to any function. Any function can access and modify global variables. Global variables are automatically initialized to 0 at the time of declaration. Global variables are generally written before main() function.

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include void func_1(); void func_2(); int a, b = 10; // declaring and initializing global variables int main() { printf("Global a = %d\n", a); printf("Global b = %d\n\n", b); func_1(); func_2(); // signal to operating system program ran fine return 0; } void func_1() { printf("From func_1() Global a = %d\n", a); printf("From func_1() Global b = %d\n\n", b); } void func_2() { int a = 5; printf("Inside func_2() a = %d\n", a); } 

Expected Output:

 1 2 3 4 5 6 7 Global a = 0 Global b = 10 From func_1() Global a = 0 From func_1() Global b = 10 Inside func_2() a = 5 

In line 4, a and b are declared as two global variables of type int. The variable a will be automatically initialized to 0. You can use variables a and b inside any function. Notice that inside function func_2() there is a local variable with the same name as a global variable. When there is a conflict between the global variable and local variable, the local variable gets the precedence, that's why inside the func_2() value of local variable a is printed.

Unlike local variables, global variables are not destroyed as soon as the function ends. They are available to any function until the program is executing.

## Static variables #

A Static variable is able to retain its value between different function calls. The static variable is only initialized once, if it is not initialized, then it is automatically initialized to 0. Here is how to declare a static variable.

Syntax: static type var_name;

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include void func_1(); int a, b = 10; int main() { func_1(); func_1(); func_1(); // signal to operating system everything works fine return 0; } void func_1() { int a = 1; static int b = 100; printf("a = %d\n", a); printf("b = %d\n\n", b); a++; b++; } 

Expected Output:

 1 2 3 4 5 6 7 8 a = 1 b = 100 a = 1 b = 101 a = 1 b = 102 

In func_1(), the variable b is declared as a static. When func_1() is called for the first time b is initialized to 100, in line 22, the value of b is incremented. This new value of b will be retained the next time the func_1() is called. When func_1() is called the second time, the variable b has retained its value which was 101, line 20, proves it by printing the value of b and once again the value of b is incremented by 1. similarly, when the third time func_() is called, the value of b is 102. Note that only variable b is able to retain its value because variable b is declared as static, However, this is not the case with variable a, which is initialized every time when func_1() is called. Also, note that static variable b is initialized only once when func_1() is called for the first time.