LearnFrenzy
Sign in Take a free test

Control Instructions · Predict the Output · Question 10

Comma operator inside an if condition – output Answer, explanation & discussion

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

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int a = 7;
    if (a = 0, a + 1)
        printf("true %d\n", a);
    else
        printf("false %d\n", a);
    return 0;
}
  • A true 0 Correct
  • B false 0
  • C true 7
  • D false 7
Correct Answer: Option A

The comma operator evaluates its left side (a = 0) and then yields its right side (a + 1 = 1). The condition is 1, so the true branch runs with a now 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