Discussion
What does this print on a 32-bit target?
int i = -1;
unsigned u = 1;
printf("%s\n", i < u ? "less" : "not less");
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.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development