Scope of Variables in C

The scope of variables in C language is the portion of the program where the program can be referenced. For example, when we define a local variable in a block, it can be referenced only following its declaration in that block or in blocks nested within that block. The two identifier scopes are block scope and file scope.

Block or local scope of variables in C

A block is a set of statements enclosed within the left and right braces { and } respectively. Blocks may be nested in C, which is a block that may contain other blocks inside it. A variable declared in a block is accessible in the block and all inner blocks of that block, but not accessible outside the block.

Now, we will see some programming examples on block scope. Find the output of the below programs.

Program1

1 #include<stdio.h>
2 int main()
3 {
4   {
5     int a = 10, b = 20;
6     {
7       printf("a = %d, b = %d\n", a, b);
8       {
9         int b = 30;
10         a++;
11         b++;
12         printf("a = %d, b = %d\n", a, b);
13       }
14       printf("a = %d, b = %d\n", a, b);
15     }
16   }
17 }

At line number 5, variable a and b are declared and initialized with values 10 and 20. Later one nested block is created and print these variables value. This block is an inner block so, variables a and b are accessible. At line number 9, one new variable b is declared and initialized with the value 30. Now, a++ will increase the outer variable ‘a’ as a=10+1=11. But next line, b++ increases the inner variable b as b = 30+1=31. These values are displayed using printf. Later the inner block is completed, so inner variable b is also un-allocated from the memory. Hence last printf statement print the values of outer variables a and b.

Output:-

a = 10, b = 20
a = 11, b = 31
a = 11, b = 20

A variable of a block can only access inside that block and all its inner blocks of the block, but it can’t be accessed from another subsequent block. For example, the below program produces the compile-time error.

#include<stdio.h>
int main()
{
   {
     int a = 10, b = 20;
   }
   {
     printf("a = %d, b = %d\n", a, b); //error
   }
}
// error: ‘a’ and 'b' are undeclared (first use in this function)

Program2

Find the output of the below program.

#include<stdio.h>
int main()
{
   int x = 1, y = 2, z=3;
   printf("x = %d, y = %d, z = %d\n", x, y, z);
   {
     float x = 10;
     char y = 65;
     printf("x = %.2f, y = %c, z = %d\n", x, y, z);
     {
       int z = 20;
       printf("x = %.2f, y = %c, z = %d\n", x, y, z);
     }
   }
}

The variable x, y, and z are declared and initialized at the third line. Later one inner block is created and the variable x and y are declared and initialized. In the inner block new variable, a and b are accessible. In the inner block new variable is created. Now, inside the inner block all new variables are displayed.

Output:-

x = 1, y = 2, z = 3
x = 10.00, y = A, z = 3
x = 10.00, y = A, z = 20

Program3

The scope of a local variable is from where it is created to the end of block or function. After completation of the block or function variable is destroyed.

#include<stdio.h>
int main()
{
   //printf("i = %d\n",i);//error
   {
     int i = 10;
     printf("i = %d\n",i);
   }
   //printf("i = %d\n",i);//error
   return 0;
}

Output:-

i = 10

File Scope of Variables in C

In file scope, the identifier can be used anywhere in the current file after the declaration of the identifier. This applies to functions and all variables declared outside functions. The file scope variable is also known as a global variable. File scope identifiers may be hidden by the block scope declarations having the same.

#include<stdio.h>
static int x = 1;
void fun()
{
   x++;
   printf("Value of x in fun is: %d\n",x);
}
int main()
{
   int i ;
   for(i=1;i<=3;i++)
   {
     x++;
   }
   printf("Value of x in main is: %d\n",x);
   fun();
   printf("Value of x in main is: %d\n",x);
   return 0;
}

Output:-

Value of x in main is: 4
Value of x in fun is: 5
Value of x in main is: 5

The variable x is a static variable, so it will initialize only once in a program. Hence in every modification, the value of x is changed. This modification can be done from anywhere in the program.

Program1

Find the output of both programs?

// program1.1
#include<stdio.h>
void show()
{
   static int count = 0;
   count++;
   printf("%d\t",count);
}
int main()
{
   show();
   show();
   show();
   return 0;
}
// program1.2
#include<stdio.h>
void show()
{
   int count = 0;
   count++;
   printf("%d\t",count);
}
int main()
{
   show();
   show();
   show();
   return 0;
}

The variable in program1.1 is static so the variable has file scope and initialized only once in the program. But the variable of program1.2 has local scope and initialized every time when the function is called.

Output of program1.1 is,
1 2 3

The Output of program1.2 is,
1 1 1

Program-2

#include<stdio.h>
int i = 9;
int main()
{
   {
     int i = 2;
     i = i+1;
     printf("%d\t",i);
     {
       int i = 7;
       i = i+3;
       printf("%d\t",i);
     }
     {
       i = i+2;
     }
     printf("%d\t",i);
   }
   printf("%d",i);
   return 0;
}

Whenever you are referring a variable than search for it in immediate block, if you don’t get it then go to next outer block where this block is created and if you don’t get the variable in that block then go to the next outer block until you reach the function blocks. If you don’t get anywhere then go to the global variable and use it.

Output:-

3 10 5 9

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *