In C, all constants and variables have a defined scope. By scope we mean the accessibility and visibility of the variables at different points in the program.
SCOPE
OF VARIABLES
In
C, all constants and variables have a defined scope. By scope we mean the
accessibility and visibility of the variables at different points in the
program. A variable or a constant in C has four types of scope: block,
function, program, and file.
We
have studied that a statement block is a group of statements enclosed within
opening and closing curly brackets ({}). If a variable is declared within a
statement block then as soon as the control exits that block, the variable will
cease to exist. Such a variable also known as a local variable is said to have
a block scope.
So
far we had been using local variables. For example, if we declare an integer x
inside a function, then that variable is unknown to the rest of the program
(i.e., outside that function).
Programming Tip:
It is an error to use the name of a function argument as the name of a local
variable.
Blocks
of statements may be placed one after the other in a program; such blocks that
are placed at the same level are known as parallel
blocks. However, a program may also contain a nested block, like a while
loop inside main (). If an integer x
is declared and initialized in main(),
and then re-declared and re-initialized in a while loop, then the integer variable x will occupy different
memory slots and will be considered as different variables. The following code
reveals this concept:
#include <stdio.h>
int main()
{
int x = 10;
int i=0;
printf("\n The value of x
outside the while loop is %d", x);
while (i<3)
{
int x = i;
printf("\n The value of x
inside the while loop is %d", x);
i++;
}
printf("\n The value of x
outside the while loop is %d", x);
return 0;
}
Output
The value of x outside the while
loop is 10
The value of x inside the while
loop is 0
The value of x inside the while
loop is 1
The value of x inside the while
loop is 2
The value of x outside the while
loop is 10
Programming Tip:
Try
to avoid variable names that hides variables in outer scope.
You
may get an error message while executing this code. This is because some C
compilers make it mandatory to declare all the variables first before you use
them, i.e., they permit declaration of variables right after the curly brackets
of main() starts.
Hence,
we can conclude two things:
•
Variables declared with same names as those in outer block mask the outer block
variables while executing the inner block.
•
In nested blocks, variables declared outside the inner blocks are accessible to
the nested blocks, provided these variables are not re-declared within the
inner block.
Note
In
order to avoid error, a programmer must use different names for variables not
common to inner as well as outer blocks.
Function
scope indicates that a variable is
active and visible from the beginning to the end of a function. In C, only the goto label has function scope. In other
words function scope is applicable only with goto label names. This means that the programmer cannot have the same
label names inside a function.
Using
goto statements is not recommended as
it is not considered to be a good programming practice. We will not discuss the
goto statement in detail here but we will
take a look at an example code that demonstrates the function scope.
int main()
{
.
.
loop: /* A goto label has function scope
*/
.
.
.
goto loop; /* the goto statement */
.
.
return 0;
}
In
this example, the label loop is
visible from the beginning to the end of the main () function. Therefore, there
should not be more than one label having the same name within the main() function.
Till
now we have studied that variables declared within a function are local
variables. These local variables (also known as internal variables) are
automatically created when they are declared in the function and are usable
only within that function. The local variables are unknown to other functions
in the program. Such variables cease to exist after the function in which they
are declared is exited and are re-created each time the function is called.
Programming Tip:
A
global variable can be used from anywhere in the program whereas the local
variable cease to exist outside the function in which it is declared.
However,
if you want a function to access some variables which are not passed to it as
arguments, then declare those variables outside any function blocks. Such
variables are commonly known as global
variables and can be accessed from any point in the program.
Lifetime
Global variables created at the beginning of program execution and remain in
existence throughout the period of execution of the program. These variables
are known to all the functions in the program and are accessible to them for
usage. Global variables are not limited to a particular function so they exist
even when a function calls another function. These variables retain their value
so that they can be used from every function in the program.
Place of Declaration
The global variables are declared outside all the functions including main ().
Although there is no specific rule that states where the global variables must
be declared, it is always recommended to declare them on top of the program
code.
Name Conflict
If we have a variable declared in a function that has same name as that of the
global variable, then the function will use the local variable declared within
it and ignore the global variable. However, the programmer must not use names
of global variables as the names of local variables, as this may lead to confusion
and erroneous result.
Note
If
a global variable is not initialized during its declaration then it is
automatically initialized to zero by default.
Consider
the following program.
#include <stdio.h>
int x = 10;
void print();
int main()
{
printf("\n The value of x in
the main() = %d", x);
int x = 2;
printf("\n The value of local
variable x in the main() = %d", x);
print();
return 0;
}
void print()
{
printf("\n The value of x in
the print () = %d", x);
}
Output
The value of x in the main() = 10
The value of local variable x in
the main() = 2
The value of x in the print() = 10
From
the code we see that local variables overwrite the value of global variables.
In big programs use of global variables is not recommended until it is very
important to use them because there is a big risk of confusing them with any
local variables of the same name.
Note
Functions
are considered to be self-contained and independent modules. So they need to be
isolated from the rest of the code, but using global variables goes against
this idea behind creating independent functions.
When
a global variable is accessible until the end of the file, the variable is said
to have file scope. To allow a variable to have file scope, declare that
variable with the static keyword
before specifying its data type:
static int x = 10;
A
global static variable can be used anywhere from the file in which it is
declared but it is not accessible by any other file. Such variables are useful
when the programmer writes his own header files.
Programming in C: Unit III (a): Functions : Tag: : Block, Function, Program, File Scope | Programming in C - Scope of Variables
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation