ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
static int privateCounter = 0;
void bump(void) { privateCounter++; }
int getCounter(void) { return privateCounter; }
int main(void) {
    bump();
    bump();
    printf("%d", getCounter());
}
  1. A. 2
  2. B. 0
  3. C. 1
  4. D. Compile-time error: privateCounter cannot be accessed by two different functions

Correct answer: A. 2

static at file scope gives privateCounter internal linkage (confined to this translation unit), but it is still an ordinary, freely-shared global WITHIN that same file — any function in the file, including bump() and getCounter(), can read and modify it normally. Two bump() calls bring it to 2.

More C Programming questions

Exams that ask this topic