Scope rules in C
Last Updated :
06 Jul, 2023
The scope of a variable in C is the block or the region in the program where a variable is declared, defined, and used. Outside this region, we cannot access the variable and it is treated as an undeclared identifier.
- The scope is the area under which a variable is visible.
- The scope of an identifier is the part of the program where the identifier may directly be accessible.
- We can only refer to a variable in its scope.
- In C, all identifiers are lexically(or statically) scoped.
Example
C
#include <stdio.h>
int main()
{
int var = 34;
printf ( "%d" , var);
return 0;
}
void func() { printf ( "%d" , var); }
|
Output
solution.c: In function 'func':
solution.c:15:28: error: 'var' undeclared (first use in this function)
void func() { printf("%d", var); }
Here, we tried to access variable names var As we can see that if we try to refer to the variable outside its scope, we get the above error.
Types of Scope Rules in C
C scope rules can be covered under the following two categories:
- Global Scope
- Local Scope
Let’s discuss each scope rule with examples.
1. Global Scope in C
The global scope refers to the region outside any block or function.
- The variables declared in the global scope are called global variables.
- Global variables are visible in every part of the program.
- Global is also called File Scope as the scope of an identifier starts at the beginning of the file and ends at the end of the file.
Example
C
#include <stdio.h>
int global = 5;
void display()
{
printf ( "%d\n" , global);
}
int main()
{
printf ( "Before change within main: " );
display();
printf ( "After change within main: " );
global = 10;
display();
}
|
Output
Before change within main: 5
After change within main: 10
Linkage of Variables in Global Scope
Global variables have external linkage by default. It means that the variables declared in the global scope can be accessed in another C source file. We have to use the extern keyword for that purpose.
Example of External Linkage
file1.c
C
int a;
int main( void )
{
a = 2;
}
|
file2.c
C
extern int a;
int myfun()
{
printf ( "%d" , a);
}
|
Output
2
Note: To restrict access to the current file only, global variables can be marked as static.
2. Local Scope in C
The local scope refers to the region inside a block or a function. It is the space enclosed between the { } braces.
- The variables declared within the local scope are called local variables.
- Local variables are visible in the block they are declared in and other blocks nested inside that block.
- Local scope is also called Block scope.
- Local variables have internal linkage.
Example
C
#include <stdio.h>
int main()
{
{
int x = 10, y = 20;
{
printf ( "x = %d, y = %d\n" , x, y);
{
int y = 40;
x++;
y++;
printf ( "x = %d, y = %d\n" , x, y);
}
printf ( "x = %d, y = %d\n" , x, y);
}
}
return 0;
}
|
Output
x = 10, y = 20
x = 11, y = 41
x = 11, y = 20
FAQs on Scope Rules in C
Q1. What if the inner block itself has one variable with the same name?
Answer:
If an inner block declares a variable with the same name as the variable declared by the outer block, then the visibility of the outer block variable ends at the point of the declaration by inner block.
Q2. What is the difference between the scope and lifetime?
Answer:
The scope and lifetime of a variable are often confused with one another
- The scope of the variable is the region where it is valid to refer to the variable using its name.
- The lifetime of the variable is the time between it is allocated memory and it is deleted from the memory.
Q2. What about functions and parameters passed to functions?
Answer:
A function itself is a block. Parameters and other local variables of a function follow the same block scope rules.
Q3. Can variables of the block be accessed in another subsequent block?
Answer:
No, a variable declared in a block can only be accessed inside the block and all inner blocks of this block.
Example: Accessing variable in outer blocks
C
int main()
{
{
int x = 10;
}
{
printf ( "%d" , x);
}
return 0;
}
|
Error
prog.c: In function 'main':
prog.c:8:15: error: 'x' undeclared (first use in this function)
printf("%d", x); // Error: x is not accessible here
^
prog.c:8:15: note: each undeclared identifier is
reported only once for each function it appears in
Example: Accessing variables in inner block
C
#include<stdio.h>
int main()
{
int x = 1, y = 2, z = 3;
printf ( "x = %d, y = %d, z = %d\n" ,
x, y, z);
{
int x = 10;
float y = 20;
printf ( "x = %d, y = %f, z = %d\n" ,
x, y, z);
{
int z = 100;
printf ( "x = %d, y = %f, z = %d\n" ,
x, y, z);
}
}
return 0;
}
|
Output
x = 1, y = 2, z = 3
x = 10, y = 20.000000, z = 3
x = 10, y = 20.000000, z = 100
Share your thoughts in the comments
Please Login to comment...