Scope rules in C
What Is Scope
• A scope is a region of the program where the variables can be accessed.
• A scope contains a group of statements and variables. The variables declared within a
block can be accessed only within that block.
• Programmers can declare the variables both inside and outside of block.
Types of Scope
C scope rules can be covered under the following two categories:
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.
int main()
// C program to illustrate the global scope {
#include <stdio.h> printf("Before change within main: ");
display();
// variable declared in global scope
int global = 5; // changing value of global
// variable from main function
// global variable accessed from printf("After change within main: ");
// within a function global = 10;
void display() display();
{ }
printf("%d\n", global);
}
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.
// filename: file1.c // filename: file2.c
// When this file is linked with file1.c, functions
int a; // of this file can access a
int main(void) extern int a;
{
a = 2; int myfun()
} {
printf("%d", a);
}
Output
2
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.
// C program to illustrate the local scope
#include <stdio.h>
// Driver Code
int main()
{
{ // This statement accesses
int x = 10, y = 20; // only outer block's
{ // variables
// The outer block contains printf("x = %d, y = %d\n", x, y);
// declaration of x and }
// y, so following statement }
// is valid and prints return 0;
// 10 and 20 }
printf("x = %d, y = %d\n", x, y);
{
// y is declared again,
// so outer block y is Output
// not accessible in this block
int y = 40;
x = 10, y = 20
// Changes the outer block x = 11, y = 41
// variable x to 11 x = 11, y = 20
x++;
// Changes this block's
// variable y to 41
y++;
printf("x = %d, y = %d\n", x, y);
}
Scope Resolution Operator In C
• Scope Resolution operator tells the compiler to use global variable instead of local variable.
• A variable followed by scope Resolution operator must be initialized globally. Scope
Resolution operator is represented by the symbol ::.
Scope resolution operator program
Unfortunately, C programming doesn't support scope resolution operator. So by making using C Plus Plus with an
extention .cpp to learn scope resolution operator for future use.
#include <stdio.h> //header file section
int value(); // function declaration
int a = 7; //variable a is declared globally
int main()
{ •The value of a = 5
value(); //calling a function •The value of a = 7
return 0;
}
int value()
{
int a = 5;
printf("The value of a = %d ",a);
printf("\nThe value of a = %d ",::a);
return 0;
}