ProgrammingVery hardProduct company OA

What is printed?

What is printed?
int x = 5;
int f(void) {
    int x = x + 1;
    return x;
}
int main(void) { printf("%d", f()); }
  1. A. 6, since the global x=5 is used on the right-hand side
  2. B. Undefined behavior: the local x is used (x + 1) to initialize itself before it has a determinate value
  3. C. 1, since x defaults to 0 before adding 1
  4. D. Compilation error: x used before declaration

Correct answer: B. Undefined behavior: the local x is used (x + 1) to initialize itself before it has a determinate value

The declaration `int x = x + 1;` introduces the new local x into scope starting at its own declarator, so the x on the right-hand side already refers to the NEW (not-yet-initialized) local x, not the outer global — reading its indeterminate value is undefined behavior, a classic self-initialization trap.

More C Programming questions

Exams that ask this topic