Scope rules in C Programming
Written by
You must have come across different types of variables like global, local and static variables. The prime factor that differentiates these variables is their scope.
The scope of a variable refers to that part of a program in which a defined variable can be publicly accessed and referenced.
Local Scope
Local variables have a local scope meaning, these variables can only be accessed and referenced in the function or the block of code in which it is defined.
Hence, since a local variable has a local scope, it cannot be outside.
Example
In the above-mentioned example, the ‘printa()’ function will successfully print the local variable ‘a’.
However, the printf statement in the main function of the code will throw us an error since the variable ‘a’ is local to the function printa() and hence cannot be accessed by the main function.
Let’s tweak the previously illustrated example and have a look at the piece of code below:
#include <stdio.h>
void printa(void)
{
int a = 7; //Local variable
printf("a= %d \n", a);
}
int main()
{
int a;
printa();
printf(" a= %d", a); //Garbage value
return 0;
}
Output:
a= 7
a= 0
In this case, the code executes successfully but since the local variable in the main function is not assigned any value it outputs a garbage value when printed or 0, which is typically the auto-initialized value offered by C for local and global variables.
This code makes it perfectly clear that a variable having local scope cannot be referenced outside it.
Let’s take one final example using actual and formal parameters to cement the understanding of a local scope.
#include <stdio.h>
void display(int); //function declaration
int main()
{
int a = 20;
display(a); //actual parameter
printf("a= %d", a);
return 0;
}
void display(int a)
{ // function definition, a=formal parameter
int k = 35;
a = a + 10;
printf("a= %d\n", a);
printf("k= %d\n", k);
}
Output:
a= 30
k= 35
a= 20
In the given program, a=20 is passed to the user-defined display( ) function. Now, the variable ‘a’ is a formal parameter and is local to the function block only. Hence, the changes made to a will not be reflected in the main function.
Therefore, printf() prints a = 20 when called in the main function.
Can the variable k defined in the display( ) be displayed in the main function?
Global Scope
A variable is said to have a global scope if it can be referenced by any and every function of the program.
The C language does not have a ‘global’ keyword hence, global variables are just defined outside any function.
Therefore, a variable with global scope can be accessed and referenced from anywhere in the program. Global variables are stored in a fixed memory location for every function to access it and its lifetime is the end of the program execution i.e, the memory is freed once the code is executed.
Since a variable with global scope can be used by all the functions of the program equally, it is subjected to the continuous change of value. So, there are good chances that your global variable might end up having a different value than the initialized one, at the end of your code.
Though volatile, a variable with global scope can be useful in cases where multiple functions require access to the same data.
The following example illustrates the use of a variable with global scope.
#include <stdio.h>
int a = 9; // global variable
void display(int); // function declaration
int main()
{
display(a); // actual parameter
printf("Main function: a= %d", a);
return 0;
}
void display(int a)
{ // function definition, a=formal parameter
printf("Display function: a= %d\n", a);
}
Output:
Display function: a= 9
Main function: a= 9
Here, a=9 is declared outside the scope of both the main and display functions. Hence, it is a global variable so, we are able to access ‘a’ without initializing it in the main and display functions.
Look at the following code snippet to understand the volatility of a variable with a global scope.
#include <stdio.h>
int a = 9;
void display(int); // function declaration
int main()
{
display(a); // actual parameter
printf("Main function: a= %d", a);
a = a + 10;
printf("\nMain function post change: a= %d\n", a);
display(a);
return 0;
}
void display(int a)
{ // function definition, a=formal parameter
printf("Display function: a= %d\n", a);
}
Output:
Display function: a= 9
Main function: a= 9
Main function post change: a= 19
Display function: a= 19
As you can see the global variable is first initialized to 9. However, its value is soon changed to 19 by the main function and the same is passed on to the display function. In this way, the value of a variable with global scope can be changed through the course of a code.
To sum up, there are two scopes in the C language: local and global.
- All variables one defines in a normal piece of code has a local scope and can only be accessed by the function or block in which it is defined.
- All variables with global scope can be accessed from any part of the program and are hence subjected to constant change.