...
By using different variable names globally and locally, the compiler forces the developer to be more precise and descriptive with variable names.
Exceptions
DCL01-EX1EX0: A function argument in a function declaration may clash with a variable in a containing scope provided that when the function is defined, the argument has a name that clashes with no variables in any containing scopes.
Code Block | ||||
---|---|---|---|---|
| ||||
extern int name; void f(char *name); /* Declaration: no problem here */ /* ... */ void f(char *arg) { /* Definition: no problem; arg doesn't hide name */ /* Use arg */ } |
DCL01-EX1: A temporary variable within a new scope inside of a macro can override a surrounding identifier.
Code Block | ||||
---|---|---|---|---|
| ||||
#define SWAP(type, a, b) do { type tmp = a; a = b; b = tmp; } while(0)
void func(void) {
int tmp = 100;
int a = 10, b = 20;
SWAP(int, a, b); /* Hidden redeclaration of tmp is acceptable */
} |
Risk Assessment
Reusing a variable name in a subscope can lead to unintentionally referencing an incorrect variable.
...