ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
void f(void) {
    printf("%d ", x);
}
int x = 42;
int main(void) {
    f();
}
  1. A. 42
  2. B. 0
  3. C. Compile-time error: x is used in f() before it is declared
  4. D. Compile-time error: f() is defined before x's declaration

Correct answer: C. Compile-time error: x is used in f() before it is declared

C requires an identifier to be declared (at least) before its first use in the source text being compiled; here f() references x before int x = 42; even appears in the file, with no earlier declaration (such as a forward extern int x;) in scope. This fails to compile.

More C Programming questions

Exams that ask this topic