ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
static int x = 5;
void f(void);
void f(void) { printf("%d", x); }
int main(void) { f(); }
  1. A. 0
  2. B. 5
  3. C. Compilation error: static variable used before full definition
  4. D. Undefined behavior

Correct answer: B. 5

A forward declaration of a function (void f(void);) just announces its signature; the actual definition later can freely use file-scope variables like static x, which was already fully defined with value 5 before f is ever called. Prints 5.

More C Programming questions

Exams that ask this topic