C Structs and Unions Quiz — 13 Questions with Answers

Test your understanding of C structures and unions with these 13 multiple-choice questions — struct initialization, padding, unions, endianness, and typedef. Every question has the answer and a short explanation hidden below it, and the trickier ones link to a full walkthrough with compilable code.

These questions come from our free Android app, C Programming Quiz — 150+ questions across 9 categories with score tracking, if you prefer to practise on your phone.

How to use this page: answer each question yourself before tapping Show answer. If you get one wrong, follow the linked explanation — knowing why the wrong options are wrong is what interviews test.

Question 1 of 13 (Easy)

How do you access a member 'x' of a struct variable 's'?

  1. s->x
  2. s.x
  3. s[x]
  4. s::x
Show answer

Answer: B — s.x

The dot operator '.' accesses a struct member directly. '->' is used when you have a pointer to a struct.

Question 2 of 13 (Medium)

How do you access member 'x' via a pointer 'p' to a struct?

  1. p.x
  2. (*p).x and p->x are both valid
  3. p[x]
  4. p::x
Show answer

Answer: B — (*p).x and p->x are both valid

Both (*p).x and p->x are equivalent. p->x is the idiomatic shorthand.

Question 3 of 13 (Medium)

What is the key difference between a struct and a union in C?

  1. A union stores all members simultaneously; a struct stores only one at a time
  2. A struct stores all members simultaneously; a union stores only one member at a time
  3. They are identical
  4. A struct can only hold integers; a union can hold any type
Show answer

Answer: B — A struct stores all members simultaneously; a union stores only one member at a time

In a struct, all members have their own storage. In a union, all members share the same memory — only one can hold a valid value at a time.

Question 4 of 13 (Medium)

What is sizeof a union with members 'char c' and 'int i' (sizeof(int) == 4)?

  1. 1
  2. 4
  3. 5
  4. 8
Show answer

Answer: B — 4

A union's size is the size of its largest member, plus any padding for alignment. With sizeof(int) = 4 and sizeof(char) = 1, the union is 4 bytes.

Question 5 of 13 (Hard)

What does 'typedef struct { int x; int y; } Point;' allow you to do?

  1. Declare a struct without a tag
  2. Use 'Point' instead of 'struct Point_tag' to declare variables
  3. Make x and y constant
  4. Create a pointer to the struct automatically
Show answer

Answer: B — Use 'Point' instead of 'struct Point_tag' to declare variables

typedef creates an alias. 'Point p;' is now equivalent to 'struct <tag> p;'. This removes the need to write 'struct' every time.

Question 6 of 13 (Hard)

What is struct padding?

  1. Extra zero bytes added at the end of a string in a struct
  2. Unused bytes the compiler inserts between struct members for alignment
  3. Extra members added by the linker
  4. The space between struct definitions in source code
Show answer

Answer: B — Unused bytes the compiler inserts between struct members for alignment

Processors require data to be aligned on natural boundaries. The compiler inserts padding bytes between struct members (and sometimes at the end) to satisfy alignment requirements, which can make sizeof(struct) larger than you expect.

Question 7 of 13 (Medium)

Can a struct contain a pointer to itself?

  1. No — structs cannot be self-referential
  2. Yes — this is how linked list nodes are built
  3. Only if typedef is used
  4. Only for union, not struct
Show answer

Answer: B — Yes — this is how linked list nodes are built

A struct can contain a pointer to its own type. This is the foundation of linked lists and trees: struct Node { int val; struct Node *next; };

Question 8 of 13 (Easy)

What keyword defines a new structure type?

  1. class
  2. record
  3. struct
  4. object
Show answer

Answer: C — struct

'struct' is the C keyword for defining a structure — a collection of named members that can have different types.

Question 9 of 13 (Hard)

What is a flexible array member?

  1. An array whose size is set at compile time via a constant
  2. An incomplete array at the end of a struct that allows variable-length allocation
  3. A pointer that acts like an array
  4. An array that resizes automatically
Show answer

Answer: B — An incomplete array at the end of a struct that allows variable-length allocation

C99 allows a struct to end with 'type arr[];' — a flexible array member. The struct is allocated with extra space for the array: malloc(sizeof(struct S) + n * sizeof(type)).

Question 10 of 13 (Medium)

What is an enum in C?

  1. A floating-point type with enumerated precision
  2. A set of named integer constants
  3. A special kind of struct
  4. A type alias created with typedef
Show answer

Answer: B — A set of named integer constants

enum defines a set of named integer constants. 'enum Color { RED, GREEN, BLUE };' makes RED=0, GREEN=1, BLUE=2 by default.

Question 11 of 13 (Hard)

What is the output of this code?

union U { int i; char c[4]; };
union U u;
u.i = 0x41424344;
printf("%c", u.c[0]);
  1. A
  2. D
  3. Compile error
  4. Platform-dependent
Show answer

Answer: D — Platform-dependent

Reading a union member other than the last written invokes implementation-defined behavior. On little-endian systems c[0] is 0x44 ('D'); on big-endian it is 0x41 ('A'). The answer is platform-dependent.

Full walkthrough with compilable code: read the detailed explanation.

Question 12 of 13 (Hard)

What is a bit-field in a struct?

  1. A boolean field
  2. A member that stores a specified number of bits rather than a full type's width
  3. A field that holds a pointer to bits
  4. A compiler-only optimization
Show answer

Answer: B — A member that stores a specified number of bits rather than a full type's width

Bit-fields let you pack multiple values into fewer bits: 'unsigned int flag : 1;' allocates just 1 bit. Useful for hardware registers and memory-constrained systems.

Question 13 of 13 (Easy)

What is the output of this code?

struct S { int a; int b; };
struct S s = {1, 2};
printf("%d", s.a + s.b);
  1. 12
  2. 3
  3. Compile error
  4. Undefined behavior
Show answer

Answer: B — 3

s.a is 1 and s.b is 2. Their sum is 3.

Full walkthrough with compilable code: read the detailed explanation.

How Did You Score?

All 13 correct means this topic is interview-ready — try the harder timed rounds in the C Programming Quiz app. Missed a few? The guides below cover everything these questions test.

Keep Practising

More C Quizzes