Local, Global and Static variables in C

Local Variables

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

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.

Expected Output:

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:

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.

Expected Output:

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;

Expected Output:

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.

Leave a Comment

%d bloggers like this: