ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
int x = 5;
if (x = 10)
    printf("true %d", x);
else
    printf("false %d", x);
  1. A. true 5
  2. B. false 10
  3. C. true 10
  4. D. Compile-time error: = should be == in a condition

Correct answer: C. true 10

x = 10 is an ASSIGNMENT (single =), not a comparison; it assigns 10 to x and the whole assignment expression evaluates to the assigned value, 10, which is treated as true in the if condition (this compiles fine in C, often with just a warning). So the if branch runs, with x now equal to 10, printing true 10 — a classic assignment-vs-comparison trap.

More C Programming questions

Exams that ask this topic