Discussion
Why is this loop wrong?
for (uint8_t i = 0; i < 300; i++) {
work(i);
}
Answer: A
Correct answer: the counter can never reach 300 and the loop never ends
A uint8_t holds 0 to 255, so after 255 it wraps to 0 and the condition remains true for ever. The comparison itself is well defined, since the counter is promoted to int; the fault is that the type cannot represent the bound, which is a common and hard-to-spot error in constrained code.
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