kota's memex

C has four types of scope: file, block, function prototype, and function.

int j;  // file scope of j begins 
void f(int i) {         // block scope of i begins
  int j = 1;            // block scope of j begins; hides file-scope j
  i++;                  // i refers to the function parameter
  for (int i = 0; i < 2; i++) {  // block scope of loop-local i begins
    int j = 2;          // block scope of the inner j begin; hides outer j
    printf("%d\n", j);  // inner j is in scope, prints 2
  }                     // block scope of the inner i and j ends 
  printf("%d\n", j);    // the outer j is in scope, prints 1
}  // the block scope of i and j ends 
void g(int j);          // j has function prototype scope; hides file-scope j

The scope of an object or function indentifier is determined by where it is declared. If the declaration is outside any block or parameter list, the indentifier has file scope, meaning the scope of the entire file in which it appears as well as any files included after that point.

If the declaration appears inside a block or within a list of parameters, it has block scope, meaning that the indentifier it declares is accessible only within the block.

If the declaration appears within the list of parameters in a function prototype (not part of a function definition), the indentifier has function prototype scope which terminates at the end of the function declarator.