LearnFrenzy
Sign in Take a free test

Structures, Unions, Enums · Predict the Output · Question 1

Struct designated initializers out of order – output Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 1 Back to Exercise

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

main.c
#include <stdio.h>

struct point {
    int x, y, z;
};

int main(void) {
    struct point p = {.z = 3, .x = 1};
    printf("%d %d %d\n", p.x, p.y, p.z);
    return 0;
}
  • A 1 0 3 Correct
  • B 3 1 0
  • C 1 garbage 3
  • D Compilation error
Correct Answer: Option A

Designated initializers can come in any order. Members that are not named – here y – are set to 0, as with any initializer.

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.

Popular