LearnFrenzy
Sign in Take a free test

Operators and Expressions · Predict the Output · Question 4

Logical NOT precedence: !x + 1 vs !(x + 1) 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 = 5;
    printf("%d %d %d\n", !x + 1, !(x + 1), !!x);
    return 0;
}
  • A 1 0 1 Correct
  • B 0 0 5
  • C 6 0 1
  • D 1 1 1
Correct Answer: Option A

Unary ! binds tighter than +: !x + 1 is (!5) + 1 = 0 + 1 = 1. !(x + 1) is !6 = 0. !!x turns any non-zero value into exactly 1 – a common idiom.

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