LearnFrenzy
Sign in Take a free test

C Variables · Predict the Output · Question 7

Initialising variables from earlier ones in one declaration Answer, explanation & discussion

  • Output
  • Easy
  • 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 a = 1, b = a + 1, c = a + b;
    printf("%d %d %d\n", a, b, c);
    return 0;
}
  • A 1 2 3 Correct
  • B 1 1 2
  • C 1 garbage garbage
  • D Compilation error
Correct Answer: Option A

Each declarator's scope starts right after it, and the initialisations happen in order, so b can use a and c can use both.

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