Scope of variables in C Programming

The scope is the region in any program where the defined variable  has its existence. It cannot be accessed beyond this scope.

The three places where the variables can be declared are:

1. Local variables which are inside a function or a block.
2. Global variables which are outside all the functions.
3. Formal parameters which are defined in the function parameters.

Local variables

  • Local variables are the ones that are declared inside the function or the block.
  • Only the statements which are inside this block are able to access these variables.
  • They are not known to the functions outside.

Example: Declaration of local variables

void main()
{
        int x,y;
        int c;
}

Global variables

  • The variables which are defined outside a function or block generally on top of the program are known as Global variables.
  • The value of the variable is held throughout the program.
  • They can be accessed anywhere in the program and in any function.

Example: Declaration of global variable

int n;
int main()
{
    code;
}

Fomal parameters

They are taken as local variables within the function and take a precedence over the global variables.