LearnFrenzy
Sign in Take a free test

Bitwise Operators · Predict the Output · Question 4

Precedence trap: if (x & 1 == 0) – output Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int x = 4;
    if (x & 1 == 0)
        puts("even");
    else
        puts("odd?!");
    return 0;
}
  • A even
  • B odd?! Correct
  • C Compilation error
  • D Nothing
Correct Answer: Option B

1 == 0 is evaluated first (0), then x & 0 is 0, so the condition is false even though 4 is even. Compilers warn about it. Write (x & 1) == 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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links