What is printed by this C code: int x = 5; if (x = 0) printf(“yes”); else printf(“no %d”, x);?
Correct answer D. no 0
Explanation
x = 0 is an assignment, not a comparison. It stores 0 in x and the expression's value is 0, which is false, so the else branch runs and prints the new value of x. A programmer who meant == would expect no 5. GCC and Clang warn about this pattern under -Wall, and writing the constant first, as in 0 == x, is one defensive habit.
Answer checked How we write and check questions
Discussion