Variable Scope in C Programming with Examples

A scope in C is a region of the program where the defined variable can exist, but variables cannot be accessed beyond that. There are two places in C where variables can be declared:

  1. Inside the function or in a block, which are also called local variables.
  2. Aside from all of the functions, which are also known as global variables.

Another location for the variable is in the definition of the function parameters. The function has already been described in a previous post.

The scope of an identifier in C programming is the area of the program where the identifier is directly accessible. The scope of a variable rule in C specifies the scope of that variable. For instance, local scope, global scope, or simply formal parameters.

If a variable's scope is local, it can only be accessed locally. For example, if a variable is declared within a function, its scope is limited to that function. Outside of that function, there is no such variable as that name until a new variable with the same name is defined. And if a variable's scope is global, the variable can be accessed from anywhere in the program or is universally accessible. In the case of formal parameters, it is similar to a local variable that can only be known by that function.

Use a global variable, which means define the variable globally, if you want to use the same variable throughout the program, from one function to the next and from one location to the next. And if you want to use a variable locally, it means you only want to use the variable for local work and then destroy it. Then you can use the local variable. Let's go over the C scope rules one by one, starting with local variables and working our way up to formal parameters.

Local Variables in C

Local variables are variables that are declared within a block or function. The variable is referred to as a local variable because it is declared and used locally. Local variables can only be used within the block or function where they are declared. It becomes unknown outside of that function or block, and it is destroyed after exiting that function or block.

Consider the following program as an example program demonstrating the local variable in C:

#include <stdio.h>
int main()
{
   int a;
   a = 10;
   printf("The value of 'a' outside the 'if' block = %d\n", a);
   if (a == 10)
   {
      int a;
      a = 20;
      printf("The value of 'a' inside the 'if' block = %d\n", a);
   }
   printf("The value of 'a' outside the 'if' block = %d\n", a);

   return 0;
}

This program produces the following output:

The value of 'a' outside the 'if' block = 10
The value of 'a' inside the 'if' block = 20
The value of 'a' outside the 'if' block = 10

I want to draw your attention to two conclusions from the preceding program:

Global Variables in C

Global variables are variables defined outside of a function, typically at the top of the program, i.e., above the main() function. A global variable that is used throughout the program. Throughout the program, global variables retain their values. As a result, global variables are accessible throughout the program, from outside to inside functions or blocks. In short, global variables can be accessed from anywhere in the program.

Consider the following program as an example program demonstrating the global variables in C:

#include <stdio.h>
int x = 10;          // global variable
void myFun1();
void myFun2();
void myFun3();
int main()
{
   printf("Inside 'main()' and outside 'if', the value of 'x' = %d \n", x);
   if(x > 0)
   {
      int x = 0;
      printf("Inside 'main()' and inside 'if', the value of 'x' = %d \n", x);
   }
   myFun1();
   printf("Inside 'main()' and outside 'if', the value of 'x' = %d \n", x);

   return 0;
}

void myFun1()
{
   printf("Inside 'myFun1()', the value of 'x' = %d \n", x);
   x++;              // incremented the value of 'x' by 1
   myFun2();
}
void myFun2()
{
   printf("Inside 'myFun2()', the value of 'x' = %d \n", x);
   x = 500;          // Now, "x" stores 500
   myFun3();
}
void myFun3()
{
   printf("Inside 'myFun3()', the value of 'x' = %d \n", x);
}

The output is:

Inside 'main()' and outside 'if', the value of 'x' = 10
Inside 'main()' and inside 'if', the value of 'x' = 0
Inside 'myFun1()', the value of 'x' = 10
Inside 'myFun2()', the value of 'x' = 11
Inside 'myFun3()', the value of 'x' = 500
Inside 'main()' and outside 'if', the value of 'x' = 500

C Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!