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

Discussion

What is the output of this C code?

typedef struct p
{
int x, y;
}k;
int main()
{
struct p p = {1, 2};
k k1 = p;
printf("%d\n", k1.x);
}

 

  • A.Compile time error
  • B.1
  • C.Depends on the standard

Answer: B

Correct answer: 1

Every element is used correctly. k is an alias for struct p, the variable named p shadows the tag p harmlessly because tags occupy a separate namespace, and k k1 = p; is an ordinary structure copy. So k1.x holds 1. Structure assignment copies every member, which is why no loop is needed.

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

Post your comment

Please log in or register to post a comment.