LearnFrenzy
Sign in Take a free test

C Loops · Find the Error · Question 8

Loop with a char counter that can never reach 200 Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

The program below has a bug. Which statement correctly describes it?

main.c
#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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links