Scope rule of function in c or variable scope means where the variable can be directly accessed after its declaration.
In the C programming language, the scope of a variable can be stated in three places:
Scope | Description |
---|---|
Local Variable | Inside a function or block |
Global variable | Outside of all function(can be accessed from anywhere) |
Formal Parameters | In the function parameters |
The scope of an identifier in C programming is the area of the program where the identifier can be directly accessed.
The rule of the scope of a variable in C tells what the scope of that variable is.
For instance, local scope, global scope, or just formal parameters.
Local Variables
Local Variables are Variables declared within a block or function.
It is because the variable is declared and utilized locally, it is referred to as a local variable.
Local variables can only be utilized within the block or function where they are declared.
It becomes unknown outside of that function or block and is deleted upon quitting that function or block.
Example of Local Variables in C
/* C Scope Rules - Local Variable Example */ #include<stdio.h> int main() { int a; a = 10; printf("Outer (before) a : %d\n", a); if(a==10) { int a; // this a hides the outer a a = 9; printf("Inner a : %d\n", a); } printf("Outer (after) a : %d\n", a); return 0; }
Output of the above example:
Outer (before) x : 10
Inner x : 9
Outer (after) x : 10
Explanation:
Here, the first value of an x is 10, which is outside the if block, and after declaring it, we initialized the value of an x is 9.
As a result, when we exit the if block, the value of an is deleted and the outer value, 10, is retained, as seen in the output figure. Keep this change in mind at all times.
You can test the above example here! ➡ C Online Compiler
Global Variables in C
Global Variables in C are variables defined outside of a function, usually at the start of the program, i.e. above the main() function.
Throughout the program, a global variable is known.
Example of Global Variables in C
/* C Scope Rules - Global Variable Example */ #include<stdio.h> int count; // this count variable is global int num1(void); int num2(void); int main() { count = 10; num1(); return 0; } int num1(void) { num2(); printf("count is %d", count); // this will print 10 } int num2(void) { int count; // this is local variable having first priority for(count=1; count<5; count++) { putchar('.'); } }
Output of the above example:
….count is 10
Explanation:
The global variables are used throughout the program. If you declare and initialize a variable with the same name, the local variable is given high priority.
As a result, the value assigned to the variable within the local block or functions will be retained until the block or functions is closed.
You can test the above example here! ➡ C Online Compiler
Formal Parameters in C
Function parameters, also known as formal parameters, are treated the same as local variables within that function, and they take precedence over global variables.
Example of Global Variables in C
/* C Scope Rules - Format Paramter Example */ #include<stdio.h> int x = 20; // global variable declaration int main() { /* local variable declaration in main() function */ int x = 10; int y = 20; int z = 0; printf("value of x inside main() function = %d\n", x); z = sum( x, y); printf("value of z inside main() function = %d\n", z); return 0; } int sum(int x, int y) { printf ("value of x inside sum() function = %d\n", x); printf ("value of y inside sum() function = %d\n", y); return x + y; }
Output of the above example:
value of x inside main() function = 10
value of x inside sum() function = 10
value of y inside sum() function = 20
value of z inside main() function = 30
Explanation:
Variables x and y within the function sum() in the main() function are only formal arguments, with a holding 10 and x holding 20.
As a result, the sum() function receives the values x and y.
The sum function prints the values of x and y. The sum() method, which is initialized to z, then returns the sum of x and y.
The number 30 is then displayed on the screen.
You can test the above example here! ➡ C Online Compiler
Initializing Local and Global Variables
When you define a local variable, the system does not initialize it; you must do so yourself.
When you define global variables like the following, the system will automatically initialize them:
Data Type | Initial Default Value |
---|---|
char | ‘\0’ |
int | 0 |
float | 0 |
double | 0 |
pointer | NULL |
It is important programming practice to correctly initialize variables; otherwise, your program may yield unexpected results since uninitialized variables will accept the first trash value available at their memory location.
Conclusion
If a variable’s scope is global, it means it can be accessed from anywhere in the program or from anywhere else in the world.
It signifies that there are no variables with that name outside of that function until a new variable with the same name is defined.
In the case of formal parameters, it is similar to a local variable that can only be known by that function.
PREVIOUS
NEXT