LearnFrenzy
Sign in Take a free test

Operators and Expressions · Predict the Output · Question 3

a += b -= c *= 2 – output Answer, explanation & discussion

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

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int a, b, c;
    a = b = c = 4;
    a += b -= c *= 2;
    printf("%d %d %d\n", a, b, c);
    return 0;
}
  • A 0 -4 8 Correct
  • B 8 4 8
  • C 4 0 8
  • D -4 -4 8
Correct Answer: Option A

Compound assignments are right-associative: c *= 2 makes c 8, b -= 8 makes b −4, and a += -4 makes a 0. Each variable is modified once, so 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