C language concept — are nested unions allowed in C?\n\nStatement: Nested unions are allowed.

Difficulty: Easy

Correct Answer: Correct

Explanation:


Introduction / Context:
This conceptual question asks whether a union may contain another union (directly or via a member that is itself a union) in standard C. Understanding type composition rules helps in designing compact variant data structures.


Given Data / Assumptions:

  • The statement claims nested unions are allowed.
  • No specific code is given; we consider standard C behavior.


Concept / Approach:
Standard C permits composing structs and unions from other structs and unions. A union member can be another union or a struct, and vice versa. Restrictions apply only to incomplete types and to members that would create infinite recursion by value (but pointers to the same type are fine).


Step-by-Step Solution:

1) Recall that structs and unions are aggregate types and may contain other aggregate types as members.2) A union U can have a member union V; the layout reserves enough space for the largest member.3) Self-embedding by value would be invalid due to infinite size, but nesting distinct unions is valid.


Verification / Alternative check:
Example compiles: union V { int x; float y; }; union U { union V v; int k; } u; This is valid in standard C.


Why Other Options Are Wrong:

  • Incorrect / C++ only / extensions only: These contradict the standard's allowance for aggregate nesting.


Common Pitfalls:
Confusing “nested by value” with “self-referential by pointer”; only the latter avoids infinite size problems.


Final Answer:
Correct

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion