![]() |
VOOZH | about |
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.
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.
The global scope refers to the region outside any block or function.
Before change within main: 5 After change within main: 10
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 theexternkeyword for that purpose.
Example of External Linkage
file1.c
main.c
Output
2Note: To restrict access to the current file only, global variables can be marked as static.
The local scope refers to the region inside a block or a function. It is the space enclosed between the { } braces.
x = 10, y = 20 x = 11, y = 41 x = 11, y = 20