Block scope : variable declared within a block {}, also called local variables, Function scope : only the goto label has function scope Program scope : variable declared outside a function, also called global variables File scope : global variable declared with the static specifier is said to have file scope
To solve a complex problem in practice, the programmer normally breaks the problem into smaller pieces and deals with each piece of the problem by writing one or two . Then, all the functions are put together to form a complete program that can be used to solve the complex problem.
In the complete program, there might be variables that have to be shared by all the functions. On the other hand, the use of some other variables may be limited to only certain functions. That is, the visibility of those variables is limited, and values assigned to those variables are hidden from many functions.
In C, we can declare a variable and indicate its visibility level by designating its scope. Thus, variables with local scope can only be accessed within the block in which they are declared. There are four categories of Variable Scope.
A block refers to any sets of statements enclosed in braces ({ and }). A variable declared within a block has block scope. Thus, the variable is active and accessible from its declaration point to the end of the block. Sometimes, block scope is also called local scope.
We can also declare variables within a nested block. If a variable declared in the outer block shares the same name with one of the variables in the inner block, the variable within the outer block is hidden by the one within the inner block for the scope of the inner block.
Example of Block Scope
int main()
{
int i; /* block scope */
.
.
.
return 0;
}
Example of Nested Block Scope
#include <stdio.h>
main()
{
int i = 32; /* block scope 1*/
printf("Within the outer block: i=%d\n", i);
{ /* the beginning of the inner block */
int i, j; /* block scope 2, int i hides the outer int i*/
printf("Within the inner block:\n");
for (i = 0, j = 10; i <= 10; i++, j--)
printf("i=%2d, j=%2d\n", i, j);
} /* the end of the inner block */
printf("Within the outer block: i=%d\n", i);
return 0;
}
Output
Within the outer block: i=32
Within the inner block:
i= 0, j=10
i= 1, j= 9
i= 2, j= 8
i= 3, j= 7
i= 4, j= 6
i= 5, j= 5
i= 6, j= 4
i= 7, j= 3
i= 8, j= 2
i= 9, j= 1
i=10, j= 0
Within the outer block: i=32
Function scope indicates that a variable is active and visible from the beginning to the end of a function.Function scope is applicable to goto labels only. A label declared is used as a target to goto statement and both goto and label statement must be in same function.
Example of Function Scope
int main()
{
int i; /* block scope */
.
.
start: /* A goto label has function scope */
.
.
goto start; /* the goto statement */
.
.
return 0;
}
A variable is said to have program scope when it is declared outside a function. Variables with program scope are also called global variables, which are visible among different files. These files are the entire source files that make up an executable program. Note that a global variable is declared with an initializer outside a function.
Example of Program Scope
int x = 0; /* program scope */
float y = 0.0; /* program scope */
int main()
{
int i; /* block scope */
.
.
.
return 0;
}
A global variable declared with the static specifier is said to have file scope. A variable with file scope is visible from its declaration point to the end of the file. Here the file refers to the program file that contains the source code. Most large programs consist of several program files.
Example of File Scope
int x = 0; /* program scope */
static int y = 0; /* file scope */
static float z = 0.0; /* file scope */
int main()
{
int i; /* block scope */
.
.
.
return 0;
}