The scope of identifiers is a fundamental concept that defines where in the program variables, functions, or other entities are accessible. The issue of managing visibility has a rich history—starting with the early implementations of C, improper use of scopes led to elusive bugs related to shadowing, unexpected behavior, and linking errors.
C was originally designed for small projects where the entire program was placed in one file. As the language evolved, there arose the necessity to clearly separate variables/functions for different parts of the program, leading to the formalization of scopes: block, file, and global.
Without a properly organized scope, it is easy to accidentally change the values of variables used in different parts of the program, encounter name conflicts, or lose control over the program's structure. Errors with "shadow" variables and overriding global definitions with locals are common sources of bugs.
In C, scopes can be:
{ ... } (for example, in a function or loop). Outside the block, the variable is "forgotten".Sample code:
#include <stdio.h> int global = 10; // global scope void foo() { int block_var = 5; // block scope static int static_file_var = 0; // file scope, if static outside functions printf("%d ", block_var); } int main() { printf("%d ", global); // global is visible foo(); // printf("%d ", block_var); // error: block_var is not visible return 0; }
Key features:
1. If a global variable and a function parameter have the same name, which one will be used inside the function?
The function "shadows" the global variable with the parameter, so inside the function the parameter's value is used. The global variable is only accessible by a different name (if it is not overridden).
2. Is the scope of static inside a function the same as static outside a function?
No! Static inside a function (static local) holds its value between calls but is only visible in that function. Static outside functions limits the visibility of the variable/function to the current file.
Sample code:
static int a = 0; // static file scope void foo() { static int b = 0; // static local scope }
3. Can you use a local variable name that matches a global one?
Yes, but this will lead to "shadowing" the global within the current block. This can lead to errors due to incorrect access to the wrong value.
Sample code:
int var = 10; void f() { int var = 20; printf("%d", var); // prints 20, global is invisible }
A project split into 2 files. Identical global variables declared in both files without static/extern. The linker throws an error or the program works with unexpected values.
Pros:
Cons:
Static and extern are clearly used, variables are placed in a separate header, naming rules are described.
Pros:
Cons: