Variables in C is a named memory location which can store values. Variables in C are of two types – Global and Local variable.

Let’s discuss both of them.

Local Variable

Local variables in C is those variables which are declared inside the scope (i.e inside {}). A Scope can be a function, loop, block, structure or anything which has its own body (i.e {}).

Since it is declared inside a scope so it is only accessible inside that particular scope only.



You can observe that the local2 variable inside while loop cannot be accessed outside the scope of a while.

As a conclusion, we can say that the local variable has limited scope and nested scope further decreases the scope of the variable, if declared inside it.

GLobal Variable

The global variable in C is those variables that are not bounded by any scope and are usually declared below header file inclusion statements.

These variables can be accessed anywhere throughout the program inside any function and with the use of extern, a global variable can also be accessed in different files.



It is because the int variable ‘global’ is declared globally that we are able to use it in print_global() as well as in main() method.

Can Local and Global Variable have Same Name?

Yes, a global and local variable can have the same name but in that case, the variable with the local scope (i.e local variable) overrides the global variable.

This means that the reference to the global variable will be lost where the local variable has the same name as the global variable.

Hope you got a little idea about the global and local variables of C language. If you have any doubts or suggestion then please comment below.

Leave a Reply

17 + seventeen =