LearnFrenzy
Sign in Take a free test

Declarations and Initializations · Predict the Output · Question 11

Two variables of an unnamed struct type Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 11 Back to Exercise

What is the output of the following C program on a 64-bit system?

main.c
#include <stdio.h>

int main(void) {
    struct { int a, b; } s1 = {1, 2}, s2;
    s2 = s1;
    s2.b = 9;
    printf("%d %d\n", s1.b, s2.b);
    return 0;
}
  • A 2 9 Correct
  • B 9 9
  • C 2 2
  • D Compilation error
Correct Answer: Option A

Both variables are declared in one declaration, so they share the same (unnamed) type and can be assigned. The assignment copies, so changing s2 leaves s1 alone.

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