A variable in any programming language is a place holder for data used in the program. When data is to be used in the program, we store them in some memory location and name that location for easier access.
The naming of addresses is known as variables. Variable is the name of the memory location. Unlike constant, the value of the variable can be changed during the execution of the program.
Scope of variable:
Scope of a variable decides two important aspects of that variable:
- Visibility of that variable
- The lifespan of that variable
By visibility, we mean which part of the program can use or see that variable, and by lifetime we mean for how long this variable exists.
The variables in C programming language may have three scopes:
- Local scope
- Program/Global scope
- File scope
Local Scope:
By local scope, we mean a variable that is defined inside a function or a block of code. The block of code is referred to some kind of loop or a condition. These variables are visible/accessible only inside the function or that block of code and their lifetime or exist until that function is under execution.
Once the function or that block of code is over, even those variables go out of existence. We can use such variables inside that function or block the code and cannot be used once that function execution is over.
Program scope/Global scope:
This is referred to as a global variable or global scope. This variable is accessible throughout the program and exists until the program terminates.
File scope:
This is also a global scope and such variables are accessible throughout the program and exist until the program terminates.
The key difference between global scope and file scope is that variables with file scope cannot be used outside that file whereas variables with program scope or global scope can be used outside that file.
Variables with file scope are preceded by a special keyword “static” unlike the program scope variables.
We will understand this scope with a simple C program:
#include <stdio.h> int global_var = 10; void func() { printf("\n Global variable is : %d", global_var ); } int main() { int local_var = 20; printf("\n Global variable is : %d", global_var ); printf("\n Local variable is : %d", local_var); func(); return 0; }
OUTPUT:
Global variable is : 10
Local variable is: 20
In the above we have a global variable name global_var and a local variable name local_var. So we can see that global variable can be used throughout the program that is inside the main function and even inside the other function name “func”.
Now let us look at another program where local variables are seen to be accessible only inside the function where they are defined and if we try to access outside that, the compiler throws the error.
#include <stdio.h> int global_var = 10; void func() { printf("\n Global variable is : %d", global_var ); printf("\n Local variable : %d", local_var ); } int main() { int local_var = 20; printf("\n Global variable is : %d", global_var ); printf("\n Local variable is : %d", local_var); func(); return 0; }
OUTPUT: main.c: In function ‘func’: main.c:16:38: error: ‘local_var’ undeclared (first use in this function) printf("\n Local variable : %d", local_var ); ^~~~~~~~~
Thus, before writing any program, we should be very clear about the scope of the variable used. If something is to be used only inside the function, it’s better we go with the local variable.
NOTE:
File scope that is the use of static keywords, we will cover in another section with a C example. As of now, we just need to keep in mind that any variable with a static keyword has a file scope; cannot be accessed outside the file, unlike the global variable.
Relevant posts:
Categories: C Language
Leave a Reply