Question 8
Back to Exercise
The program below has a bug. Which statement correctly describes it?
#include <stdio.h>
int main(void) {
int count = 0;
for (signed char c = 0; c < 200; c++)
count++;
printf("%d\n", count);
return 0;
}
- A signed char cannot be used in a loop
- B A signed char holds at most 127, so c < 200 is always true and the loop never ends Correct
- C count overflows
- D There is no bug; it prints 200
Correct Answer: Option B
After 127, c++ converts 128 back to a signed char, which wraps (implementation-defined) to −128, so the condition never fails. Compilers warn that the comparison is always true. Use an int counter.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!