LearnFrenzy
Sign in Take a free test

Decision Making · Find the Error · Question 3

Bug: if (!count == 5) Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

The code should print "not five" when count is not 5. What does it print for count = 3, and why?

main.c
#include <stdio.h>

int main(void) {
    int count = 3;
    if (!count == 5)
        puts("not five");
    else
        puts("five or other");
    return 0;
}
  • A not five
  • B five or other – !count is evaluated first (0 or 1), and 0 or 1 is never 5; write count != 5 Correct
  • C Nothing
  • D It does not compile
Correct Answer: Option B

Unary ! binds tighter than ==: the condition is (!3) == 5, i.e. 0 == 5, always false. Compilers warn that the logical not applies only to the left side.

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