Machine-translated by Google. Questions are authored in English.
Top

Discussion

What does this print on a 32-bit target?

int      i = -1;
unsigned u =  1;
printf("%s\n", i < u ? "less" : "not less");

  • A.the program will not compile at all
  • B.not less
  • C.less
  • D.the behaviour is undefined and nothing can be said

Answer: B

Correct answer: not less

The usual arithmetic conversions turn the signed operand into unsigned when the two have equal rank, so \( -1 \) becomes 4,294,967,295 and is not less than 1. The code compiles and its behaviour is perfectly defined - it simply does something other than what it appears to say, which is why compilers warn about mixed comparisons.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.