Question 1
Back to Exercise
The program below has a bug. Which statement correctly describes it?
#include <stdio.h>
int main(void) {
int a[3] = {1, 2, 3};
for (unsigned i = 2; i >= 0; i--)
printf("%d ", a[i]);
printf("\n");
return 0;
}
- A a must be declared unsigned
- B i >= 0 is always true for an unsigned i; after 0 it wraps to 4294967295 and a[i] is far out of bounds Correct
- C printf cannot print inside a loop
- D There is no bug; it prints 3 2 1
Correct Answer: Option B
Unsigned values cannot be negative, so the loop never ends by itself; the next index after 0 is UINT_MAX and the read crashes. Write for (unsigned i = 3; i-- > 0; ).
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!