Practice mode C Language

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.