Machine-translated by Google. Questions are authored in English.
Top

Discussion

Predict the output of following C program

#include <stdio.h>
struct Point
{
  int x, y, z;
};
 
int main()
{
  struct Point p1 = {.y = 0, .z = 1, .x = 2};
  printf("%d %d %d", p1.x, p1.y, p1.z);
  return 0;
}

 

  • A.Compiler Error
  • B.
    2 0 1
  • C.
    0 1 2
  • D.
    2 1 0

Answer: B

Correct answer: 2 0 1

These are designated initialisers, introduced in C99: each value names the member it initialises, so their order in the braces is irrelevant. .x = 2, .y = 0, .z = 1 whatever order they are written in, and the program prints 2 0 1.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.