C Scope of a Variable

Scope refers to the visibility of variables. In other words, a scope is a region of a program, and the scope of variables refers to the region of the program where the variables can be declared and used in the program.
Depending on the region, there are three types of scope that are declared and used:

  • Local variables are defined inside a function or a block.
  • Global variables are outside of all functions.
  • Formal parameters, these are In the definition of function parameters.

Let’s look at them individually:

Local Variable

Variables that are declared inside a function or block are local variables. The scope of local variables will be within the function only. These variables are declared within the function and can’t be accessed outside the function.


Global variables 

Global variables are declared outside any functions, usually at the top of the program.
They can be used later by the blocks of code:


Formal Parameters 

Formal Parameters are the parameters which are used inside the body of a function. Formal parameters are treated as local variables in that function and get a priority over the global variables.



Question-related to Scope of a Variable