LearnFrenzy
Sign in Take a free test

Operators and Expressions · Predict the Output · Question 7

r = x++ || x++ – output Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int x = 0;
    int r = x++ || x++;
    printf("%d %d\n", r, x);
    return 0;
}
  • A 1 2 Correct
  • B 0 1
  • C 1 1
  • D 0 2
Correct Answer: Option A

|| has a sequence point after its left operand. The first x++ yields 0 (false) and makes x 1, so the right side runs: it yields 1 (true) and makes x 2. Unlike i = i++, this is well-defined.

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