Scope of Variable in C

Shashwat Raj
1 min readAug 9, 2019

--

Scope of Variable is the part of the program where the identifier (or variable) may directly be accessible. In C, all identifiers are statically scoped. The variable is alive in its scope only and cannot be accessed outside it.

So there are two types of scopes depending on the region where variables are declared and used:

Local Variable

Variables that are declared inside a function or a block are called local variables and are said to have local scope. These local variables can only be used or access within the function or block in which these are declared. It is invalid outside it.

A program to show the scope of local variables

Global Variable

Variables that are defined outside of all the functions and are accessible throughout the program are global variables and are said to have global scope. Once declared, these can be accessed and modified by any function in the program. We can have the same name for a local and a global variable but the local variable gets priority inside a function.

A program to show the scope of the global variable
Table Showing the Default Value of Global Variable if uninitialized

--

--

Shashwat Raj

Honours Computer Science Student & Enthusiast working with Python | Data Science | Machine Learning | Artificial Intelligence | Web Development