Structure and union interview questions in c : Master Practice Set #1

On: December 7, 2025
Structure and union interview questions in c

Prepare for technical interviews with our Structure and union interview questions in C practice set. This guide covers memory allocation, padding, arrays in structures and unions, differences, and real-world examples. Ideal for beginners and embedded systems developers to master structures and unions concepts with detailed explanations and practice exercises.

If you are preparing for a C programming interview, understanding structures and unions is essential. These user-defined data types allow you to group different types of data under a single name, making your programs organized and memory-efficient.

This practice set #1 contains commonly asked questions on structures and unions in C along with detailed explanations. Whether you are a beginner or brushing up your C skills for interviews, this guide will help you gain confidence.

Why Structures and Unions Are Important in C?

  • Structures are used to group different data types together to represent a real-world object.
  • Unions are used to store different data types in the same memory location, saving memory.
  • Both are widely asked in technical interviews, especially in embedded systems, data structures, and low-level programming roles.

Pro Tip: Interviewers often ask differences, memory allocation, and practical use cases.

A structure in C is a user-defined data type that allows you to group variables of different data types under a single name.
It is used when you need to represent a complex object with multiple attributes.

Definition

A structure is declared using the keyword struct.

Syntax

struct structure_name {
    data_type member1;
    data_type member2;
    ...
};

Example

struct Student {
    int roll;
    float marks;
    char name[20];
};

Here:

  • roll → integer
  • marks → float
  • name → character array

All combined into one unit called Student.

Why do we use structures?

NeedDescription
Grouping different data typesLike name, age, salary together
Represents real-world objectsLike employee, car, product
Easier data handlingEspecially in arrays of structures
Used in embedded systemsRegisters, packets, device configs

How to declare and access structure variables?

Declaring

struct Student s1;

Accessing members

s1.roll = 101;
s1.marks = 89.5;
strcpy(s1.name, "Jos");

Memory Concept

A structure stores its members in sequence, but compiler may add padding to follow alignment rules

A union in C is a user-defined data type similar to a structure, but with one important difference:

All members of a union share the same memory location.
So the size of a union = size of its largest member.

Definition

A union is declared using the keyword union.

Syntax

union union_name {
    data_type member1;
    data_type member2;
    ...
};

Example

union Data {
    int i;
    float f;
    char ch;
};

Here, i, f, and ch all use the same memory.

Key Concept: Shared Memory

Only one member holds a valid value at a time.

For example:

union Data d;
d.i = 10;
d.f = 20.5;

When you assign f, it overwrites the memory that was holding i.

Memory Allocation

If members are:

  • int → 4 bytes
  • float → 4 bytes
  • char → 1 byte

Union size = 4 bytes (max size).

Union vs Structure

FeatureStructureUnion
MemoryEach member has its own memoryAll members share one memory
SizeSum of all membersSize of largest member
Use caseStore multiple valuesAccess different data types in same memory
Member validityAll members valid at onceOnly one member valid at a time

Where is a union used?

Common in embedded systems:

  • Memory-critical applications
  • Interpreting the same memory as different data types

(e.g., byte-wise access to registers or network packets)

Example:

union Packet {
    int full;
    char byte[4];
};

Structures and unions are both user-defined data types in C, but they differ mainly in memory allocation and usage behavior.

1. Memory Allocation

Structure

  • Each member gets its own memory.
  • Total size = sum of all members (with padding).

Union

  • All members share the same memory.
  • Total size = size of the largest member.

Example:
If you have int (4B) + float (4B) + char (1B):

  • Structure size ≈ 9–12 bytes (due to padding)
  • Union size = 4 bytes

2. Member Storage Behavior

Structure

  • All members hold values independently.
    You can use all at the same time.

Union

  • Only one member holds a valid value at a time.
    Writing to one member overwrites others.

3. Memory Efficiency

Structure → Not memory efficient

Union → Highly memory efficient

Used in embedded systems where memory is limited.

4. Use Case

Structure

Used when you need to store multiple attributes together.

Examples:

  • Employee record
  • Student record
  • Configuration parameters

Union

Used when you want to interpret the same memory in different ways.

Examples:

  • Hardware register access
  • Protocol packets
  • Memory-saving applications
  • Type punning

5. Initialization

Structure

  • Can initialize multiple members at once.

Union

  • Can initialize only one member at a time.

Tabular Difference (Easy to Remember)

FeatureStructureUnion
MemoryEach member has its own memoryShared memory
SizeSum of all membersSize of largest member
Member validityAll members validOnly one valid at a time
Use caseMultiple attributesMemory sharing / reinterpretation
AccessNo overwritingOverwrites other members
EfficiencyLess efficientVery efficient
InitializationMultiple membersOnly one member

Memory allocation in a structure in C follows a very specific and important rule:

Memory in a Structure = Sequential allocation + Alignment + Padding

Let’s break it down clearly.

1. Members are stored in sequence

Structure members are stored in the same order as they are declared.

Example:

struct A {
    char c;     // 1 byte
    int x;      // 4 bytes
    char d;     // 1 byte
};

Memory layout (conceptually):

[c][padding][padding][padding][x x x x][d][padding][padding][padding]

2. Alignment requirement

Each data type must be stored at an address that is a multiple of its alignment boundary.

Common alignments:

  • char → 1 byte
  • short → 2 bytes
  • int → 4 bytes
  • float → 4 bytes
  • double → 8 bytes (on many systems)

3. Padding is added automatically

Padding bytes are inserted to ensure alignment of next members.

Example with Detailed Memory Calculation

Structure:

struct A {
    char c;     // 1 byte
    int x;      // 4 bytes
    char d;     // 1 byte
};

Step-by-step memory allocation:

Member 1: char c

  • Size = 1 byte
  • Stored at offset 0

Padding before next member

Next member = int
int needs 4-byte alignment
Current offset = 1
Next multiple of 4 = 4
So compiler inserts 3 bytes padding

Member 2: int x

  • Stored at offset 4 → 7
  • Size = 4 bytes

Member 3: char d

  • Stored at offset 8
  • Size = 1 byte

Padding at the end (structure alignment)

Structure must be aligned to the size of its largest member (here, 4 bytes).
Current size = 9 bytes
Next multiple of 4 = 12
So compiler adds 3 bytes padding at end.

Final Structure Size = 12 bytes

General Rules of Structure Memory Allocation

  • Members follow declared order
  • Compiler inserts padding between members
  • Compiler may insert padding at the end
  • Structure size = Next multiple of largest member alignment

Important Interview Point

Structure padding and alignment are compiler-dependent, but this is how GCC/Clang commonly work.

Memory allocation in a union is quite different from a structure. Let’s break it down carefully.

Key Concept of Union Memory Allocation

  • All members of a union share the same memory location.
  • Size of a union = size of its largest member (plus any padding needed for alignment).
  • Only one member can hold a valid value at a time.

Example

union Data {
    int i;      // 4 bytes
    float f;    // 4 bytes
    char ch;    // 1 byte
};

Step-by-step Memory Allocation

  1. Member int i → needs 4 bytes
  2. Member float f → also 4 bytes
  3. Member char ch → 1 byte

All members share the same memory:

| i (4B) / f (4B) / ch (1B) |
  • Total union size = 4 bytes (size of largest member)
  • Accessing ch overwrites the same memory that i or f uses.

Rules of Union Memory Allocation

  1. Shared Memory: All members occupy the same memory block.
  2. Size = Largest Member: Compiler calculates the union size based on the largest member, plus padding for alignment if needed.
  3. Single Valid Member: At any time, only one member can hold meaningful data.
  4. Alignment Rules Apply: Memory alignment rules are followed for the largest member.

Example in Action

union Data d;

d.i = 100;       // store int
printf("%d", d.i);  // 100
d.f = 10.5;      // store float, overwrites int
printf("%d", d.i);  // undefined, memory overwritten

Difference with Structure

FeatureStructureUnion
MemoryEach member has its own memoryAll members share same memory
SizeSum of all membersSize of largest member
Member validityAll members validOnly one member valid

Yes, we can declare arrays inside a structure in C. In fact, arrays are commonly used as structure members to store multiple values under a single field.

Syntax

struct StructureName {
    data_type array_name[array_size];
    // other members
};

Example 1: Array of integers inside a structure

#include <stdio.h>

struct Student {
    char name[20];   // character array
    int marks[5];    // integer array
};

int main() {
    struct Student s1;

    // Assigning values
    strcpy(s1.name, "Nish");
    s1.marks[0] = 85;
    s1.marks[1] = 90;
    s1.marks[2] = 75;
    s1.marks[3] = 88;
    s1.marks[4] = 92;

    // Printing values
    printf("Name: %s\n", s1.name);
    printf("Marks: ");
    for(int i=0; i<5; i++)
        printf("%d ", s1.marks[i]);

    return 0;
}

Output:

Name: Nish
Marks: 85 90 75 88 92

Example 2: 2D array inside a structure

struct Matrix {
    int mat[3][3]; // 3x3 matrix
};

Memory Consideration

  • Array inside a structure occupies contiguous memory like a normal array.
  • If the structure has other members, padding may be added for alignment.
  • You can also have arrays of structures, which is common in embedded systems and data handling.

Example 3: Array of structures

struct Student students[3];  // Array of 3 Student structures

Memory Allocation for Arrays Inside a Structure

When you declare an array inside a structure, the array occupies contiguous memory just like a normal array.

Example:

struct Student {
    char name[10];  // char array
    int marks[5];   // int array
};

Memory Allocation:

  • name[10] → 10 bytes (assuming char = 1 byte)
  • marks[5] → 5 × 4 = 20 bytes (assuming int = 4 bytes)

So total size before padding = 30 bytes

Padding with Arrays in a Structure

C compilers align structure members based on the largest data type in the structure (for memory efficiency). Padding may be inserted:

Rules:

  1. Each member is aligned to its natural boundary (size of data type).
  2. Compiler may add padding between members and at the end of structure.

Example:

struct Student {
    char name[10]; // 10 bytes
    int marks[5];  // 20 bytes, int alignment = 4
};

Step-by-step layout:

  • name[10] → offset 0–9
  • Padding → next multiple of 4 → offset 10–11 (2 bytes padding)
  • marks[5] → offset 12–31

Final structure size = 32 bytes

So even with arrays, padding ensures proper alignment.

Array of Structures vs Structure with Array

Let’s clarify this common confusion.

A. Array of Structures

struct Student {
    char name[20];
    int marks;
};

struct Student students[3]; // Array of 3 Student structures
  • Memory layout: each structure is stored contiguously
  • Example (if size of one Student = 24 bytes):
| Student1 | Student2 | Student3 |
  • Each structure can have different values, accessed as students[0].marks etc.

Useful when you have multiple records.

B. Structure with Array

struct Class {
    char name[20];
    int marks[3];
};
  • Memory layout:
| name[20] | marks[0] | marks[1] | marks[2] |
  • One structure holds multiple related values in arrays.
  • All values are inside a single structure.

Useful when you have one entity with multiple properties.

Key Difference

FeatureArray of StructuresStructure with Array
MemoryMultiple structures in contiguous memorySingle structure holds array elements
Accessstudents[0].marksclass1.marks[0]
Use CaseMultiple entitiesOne entity with multiple properties

Visualization Example

Array of Structures:

Student[0]: name | marks
Student[1]: name | marks
Student[2]: name | marks

Structure with Array:

Class: name | marks[0] | marks[1] | marks[2]

Yes, we can declare arrays inside a union in C. Just like other members, arrays inside a union share the same memory with the union, and the size of the union is determined by its largest member.

Syntax

union UnionName {
    int numbers[5];
    char name[10];
};

Example

#include <stdio.h>
#include <string.h>

union Data {
    int numbers[3];
    char name[10];
};

int main() {
    union Data d;

    // Assign values to array
    d.numbers[0] = 10;
    d.numbers[1] = 20;
    d.numbers[2] = 30;

    printf("Numbers: %d %d %d\n", d.numbers[0], d.numbers[1], d.numbers[2]);

    // Assign values to char array (overwrites numbers)
    strcpy(d.name, "Nish");

    printf("Name: %s\n", d.name);
    printf("Numbers[0] after name assignment: %d\n", d.numbers[0]); // Memory overwritten

    return 0;
}

Output:

Numbers: 10 20 30
Name: Nish
Numbers[0] after name assignment: (undefined, memory overwritten)

Memory Concept

  • All members share the same memory block.
  • Size of union = size of largest member (array included).
  • Only one member can hold valid data at a time.
  • Assigning to an array overwrites previous data in the union.

Example of Union with Array and Single Variable

union Example {
    int arr[5];  // 5 integers
    int x;       // single integer
};
  • Union size = max(size of arr, size of x) → size of arr (20 bytes if int = 4B)
  • Both x and arr share the same memory.

Yes, a structure in C can hold multiple data types. That’s actually the main purpose of a structure.

Explanation

A structure is a user-defined data type that allows you to group variables of different types under a single name.

This is useful when you want to represent a real-world object with multiple attributes of different types.

Example

#include <stdio.h>
#include <string.h>

struct Student {
    int roll;        // integer
    float marks;     // float
    char name[20];   // character array
};

int main() {
    struct Student s1;

    // Assign values
    s1.roll = 101;
    s1.marks = 89.5;
    strcpy(s1.name, "Nish");

    // Print values
    printf("Roll: %d\n", s1.roll);
    printf("Marks: %.2f\n", s1.marks);
    printf("Name: %s\n", s1.name);

    return 0;
}

Output:

Roll: 101
Marks: 89.50
Name: Nish

Key Points

  • Structures can combine int, float, char, arrays, pointers, and even other structures.
  • All members are stored in the order they are declared, but padding may be added for memory alignment.
  • Ideal for modeling complex objects like employees, students, or hardware registers.

Yes, a union in C can hold multiple data types, but with an important distinction compared to structures:

Explanation

A union is a user-defined data type that allows you to define multiple members of different data types, but all members share the same memory location.

Only one member can hold a valid value at a time. Assigning a value to one member overwrites the memory of other members.

Example

#include <stdio.h>
#include <string.h>

union Data {
    int i;
    float f;
    char name[10];
};

int main() {
    union Data d;

    d.i = 100;
    printf("i = %d\n", d.i);

    d.f = 3.14;  // overwrites d.i
    printf("f = %.2f\n", d.f);
    printf("i after f assignment = %d\n", d.i); // memory overwritten

    strcpy(d.name, "Nish");  // overwrites d.f
    printf("name = %s\n", d.name);

    return 0;
}

Output:

i = 100
f = 3.14
i after f assignment = (undefined)
name = Nish

Key Points

  • A union can hold multiple data types as members.
  • Memory is shared; size = largest member.
  • Only one member is valid at a time.
  • Useful in memory-critical applications, embedded systems, or interpreting the same memory in different ways.

In C, the size of an empty structure (a structure with no members) is not zero.

Explanation

  • Even if a structure has no members, the compiler allocates at least 1 byte for it.
  • This ensures that each structure variable has a unique address in memory.
  • Without this, you couldn’t have distinct instances of the empty structure.

Example

#include <stdio.h>

struct Empty {
    // no members
};

int main() {
    struct Empty e1, e2;

    printf("Size of empty structure: %zu bytes\n", sizeof(struct Empty));
    printf("Address of e1: %p\n", (void*)&e1);
    printf("Address of e2: %p\n", (void*)&e2);

    return 0;
}

Sample Output (may vary by compiler):

Size of empty structure: 1 bytes
Address of e1: 0x7ffee1c8a9a0
Address of e2: 0x7ffee1c8a9a1

Key Points

  1. Size = 1 byte (ensures unique memory address).
  2. Different from C++, where empty structure (or class) may also have size 1 due to object identity.
  3. If you add members, the size is calculated based on member sizes + padding/alignment.

In C, the size of an empty union is also not zero, but it behaves slightly differently than a structure.

Explanation

  • Even if a union has no members, the compiler allocates at least 1 byte.
  • This ensures that each union variable has a unique address in memory, similar to structures.
  • Reason: Without at least 1 byte, you couldn’t create multiple distinct union variables.

Example

#include <stdio.h>

union EmptyUnion {
    // no members
};

int main() {
    union EmptyUnion u1, u2;

    printf("Size of empty union: %zu bytes\n", sizeof(union EmptyUnion));
    printf("Address of u1: %p\n", (void*)&u1);
    printf("Address of u2: %p\n", (void*)&u2);

    return 0;
}

Sample Output (may vary by compiler):

Size of empty union: 1 bytes
Address of u1: 0x7ffee1c8a9a0
Address of u2: 0x7ffee1c8a9a1

Key Points

  1. Size = 1 byte even if empty.
  2. Ensures unique addresses for union variables.
  3. If members are added, union size = size of largest member + padding.
  4. Same principle applies as with empty structures.

Size Comparison: Structure vs Union in C

TypeMembersSizeNotes
Empty StructureNone1 byteCompiler allocates 1 byte to give a unique address to each variable.
Empty UnionNone1 byteSame reason as structure: ensures unique memory address.
Structure with membersint, char, float, etc.Sum of member sizes + paddingPadding added for alignment of members.
Union with membersint, char, float, etc.Size of largest member + paddingAll members share the same memory.

Yes, a structure in C can contain another structure as a member. This is commonly called a nested structure.

Explanation

  • A structure can have another structure as one of its members.
  • This is useful for modeling complex objects with hierarchical data.
  • The nested structure can be accessed using the dot operator.

Syntax

struct Inner {
    int x;
    int y;
};

struct Outer {
    struct Inner point;  // nested structure
    char name[20];
};

Example

#include <stdio.h>
#include <string.h>

struct Address {
    char city[20];
    int pin;
};

struct Student {
    char name[20];
    int roll;
    struct Address addr;  // nested structure
};

int main() {
    struct Student s1;

    strcpy(s1.name, "Nish");
    s1.roll = 101;
    strcpy(s1.addr.city, "Patna");
    s1.addr.pin = 800001;

    printf("Name: %s\n", s1.name);
    printf("Roll: %d\n", s1.roll);
    printf("City: %s\n", s1.addr.city);
    printf("PIN: %d\n", s1.addr.pin);

    return 0;
}

Output:

Name: Nish
Roll: 101
City: Patna
PIN: 800001

Key Points

  1. Nested structures allow hierarchical data representation.
  2. Access members using outer.inner syntax.
  3. You can also declare arrays of nested structures.
  4. Padding and alignment rules apply for each member, including nested structures.

Yes, a union in C can contain another union as a member. This is similar to nested structures, but the memory-sharing rules of unions apply.

Explanation

  • A union can have another union as a member, along with other data types.
  • All members of the outer union share the same memory, including the nested union.
  • At any given time, only one member of the outer union is valid, whether it’s a simple variable or the nested union.

Syntax

union InnerUnion {
    int x;
    float y;
};

union OuterUnion {
    char ch;
    union InnerUnion inner;  // nested union
};

Example

#include <stdio.h>

union InnerUnion {
    int x;
    float y;
};

union OuterUnion {
    char ch;
    union InnerUnion inner;
};

int main() {
    union OuterUnion u;

    u.ch = 'A';
    printf("ch = %c\n", u.ch);

    u.inner.x = 100;  // overwrites ch
    printf("inner.x = %d\n", u.inner.x);
    printf("ch after inner.x assignment = %c\n", u.ch);  // memory overwritten

    return 0;
}

Output:

ch = A
inner.x = 100
ch after inner.x assignment = (undefined)

Key Points

  1. Memory of outer union is shared among all members, including nested unions.
  2. Only one member of the outer union is valid at a time.
  3. Useful in memory-critical applications where you want to interpret the same memory in different ways.
  4. Size of outer union = largest member (may be the nested union).

Yes, a structure in C can contain a union as one of its members. This is very common in scenarios where you want a single field to hold multiple types of data while still keeping other fixed fields in the structure.

Explanation

  • A structure can have fixed-type members and also a union member.
  • The union inside the structure shares memory for its members, but the rest of the structure’s members have their own memory.
  • Accessing members of the union is done via the dot operator for the structure and then the union member.

Syntax

union Data {
    int i;
    float f;
    char ch;
};

struct Student {
    char name[20];
    int roll;
    union Data info;  // union inside structure
};

Example

#include <stdio.h>
#include <string.h>

union Info {
    int marks;
    float percentage;
};

struct Student {
    char name[20];
    int roll;
    union Info score;  // union member
};

int main() {
    struct Student s1;

    strcpy(s1.name, "Nish");
    s1.roll = 101;

    s1.score.marks = 95;
    printf("Name: %s\nRoll: %d\nMarks: %d\n", s1.name, s1.roll, s1.score.marks);

    s1.score.percentage = 92.5;  // overwrites marks
    printf("Percentage: %.2f\nMarks after overwrite: %d\n", s1.score.percentage, s1.score.marks);

    return 0;
}

Output:

Name: Nish
Roll: 101
Marks: 95
Percentage: 92.50
Marks after overwrite: (undefined)

Key Points

  1. Structure members have their own memory, union members share memory.
  2. Only one member of the union is valid at a time.
  3. Useful in embedded systems, protocol handling, and memory-efficient applications.
  4. Size of structure = sum of other members + size of union + padding for alignment.

Yes, a union in C can contain a structure as one of its members. This is a common technique when you want a single memory location to store different types of data, including complex data types like structures.

Explanation

  • A union can have primitive types (int, char, float) and structures as members.
  • All members of the union share the same memory, including the nested structure.
  • At any time, only one member of the union is valid, whether it’s a structure or a simple type.
  • This is often used in memory-efficient programming and embedded systems.

Syntax

struct Address {
    char city[20];
    int pin;
};

union StudentInfo {
    int roll;
    struct Address addr;  // structure inside union
};

Example

#include <stdio.h>
#include <string.h>

struct Address {
    char city[20];
    int pin;
};

union StudentInfo {
    int roll;
    struct Address addr;  // structure inside union
};

int main() {
    union StudentInfo s;

    s.roll = 101;
    printf("Roll: %d\n", s.roll);

    strcpy(s.addr.city, "Patna");  // overwrites roll
    s.addr.pin = 800001;
    printf("City: %s\nPIN: %d\n", s.addr.city, s.addr.pin);
    printf("Roll after overwriting with addr: %d\n", s.roll); // memory overwritten

    return 0;
}

Output:

Roll: 101
City: Patna
PIN: 800001
Roll after overwriting with addr: (undefined)

Key Points

  1. Memory of the union is shared among all members, including the structure.
  2. Only one member of the union is valid at a time.
  3. Size of the union = size of the largest member (structure included).
  4. Useful for memory-saving techniques, embedded systems, and interpreting the same memory differently.

Conclusion: Structure and Union Interview Questions in C

After going through these 15 fundamental questions on structures and unions in C, you should now have a solid understanding of how these user-defined data types work.

Key takeaways:

  1. Structures allow you to group multiple variables of different data types together while giving each member its own memory.
  2. Unions let you store different types of data in the same memory location, saving memory but allowing only one member to be valid at a time.
  3. Understanding memory allocation, padding, and alignment is crucial, especially in embedded systems and low-level programming.
  4. Both structures and unions can be nested, contain arrays, or even include each other to model complex hierarchical data.
  5. Knowing the differences between structure and union, and how memory is shared or allocated, is a common interview topic and helps you write efficient C programs.

By practicing these questions and experimenting with memory layouts and code examples, you can confidently handle interview questions on structures and unions, as well as use them effectively in real-world C applications.

If you’re learning C programming and want to strengthen your fundamentals before practicing Structures and Unions Interview Questions, you should explore this detailed beginner-friendly guide on structures: Structures in C – Complete Guide
It explains structure syntax, memory layout, padding, nesting, and real-world examples in a very simple way. Reading this will give you a strong foundation before jumping into advanced interview questions.

1. What is a structure in C?

A structure in C is a user-defined data type that allows you to group different types of variables under a single name, making it easier to organize and manage complex data.

2. What is a union in C?

A union in C is a special data type where all members share the same memory location, allowing only one member to store a valid value at a time, which saves memory.

3. Can a structure hold multiple data types?

Yes, a structure can hold variables of different data types such as int, float, char arrays, or even other structures, allowing hierarchical and complex data representation.

4. Can a union hold multiple data types?

Yes, a union can include multiple data types, but only one member can hold a valid value at a time, because all members share the same memory space.

5. What is the difference between a structure and a union in C?

Structure: Each member has its own memory; all members can be used simultaneously.
Union: Members share memory; only one member can be used at a time.
Memory efficiency: Union is more memory-efficient than structure for storing mutually exclusive data.

6. How is memory allocated for a structure?

Memory for a structure is allocated as the sum of all member sizes plus any padding added for alignment, ensuring efficient access by the CPU.

7. How is memory allocated for a union?

Memory for a union is allocated based on the size of its largest member, as all members share the same memory space, ensuring only one member is valid at a time.

Leave a Comment