0
0
Cprogramming~20 mins

Union basics - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Union Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of union member assignment
What is the output of this C program?
C
#include <stdio.h>

union Data {
    int i;
    float f;
    char str[20];
};

int main() {
    union Data data;
    data.i = 10;
    data.f = 220.5f;
    printf("%d\n", data.i);
    return 0;
}
ASome unpredictable integer value
B220
C10
DCompilation error
Attempts:
2 left
💡 Hint
Remember that all members of a union share the same memory space.
Predict Output
intermediate
2:00remaining
Size of union vs struct
What will be the output of this program?
C
#include <stdio.h>

union U {
    int i;
    double d;
    char c;
};

struct S {
    int i;
    double d;
    char c;
};

int main() {
    printf("%zu %zu\n", sizeof(union U), sizeof(struct S));
    return 0;
}
A16 24
B16 16
C8 16
D24 24
Attempts:
2 left
💡 Hint
Union size is the size of its largest member; struct size is sum plus padding.
🔧 Debug
advanced
2:00remaining
Why does this union code cause unexpected output?
Consider this code snippet. Why does printing data.str after assigning data.i show unexpected characters?
C
#include <stdio.h>
#include <string.h>

union Data {
    int i;
    char str[4];
};

int main() {
    union Data data;
    data.i = 0x41424344; // Hex for 'ABCD'
    printf("%s\n", data.str);
    return 0;
}
ABecause the integer value is too large to fit in the union
BBecause the string is not null-terminated, printf reads beyond the array causing garbage output
CBecause union members cannot be accessed after assignment to another member
DBecause printf cannot print char arrays inside unions
Attempts:
2 left
💡 Hint
Think about how strings are represented in C and what printf expects.
🧠 Conceptual
advanced
2:00remaining
Union member alignment and padding
Which statement about union member alignment and padding is true?
AUnion size is always the sum of all member sizes including padding
BUnion members are stored in separate memory locations to avoid padding
CUnion size is the size of its largest member, aligned to the strictest member's alignment requirement
DUnion size is always equal to the smallest member size
Attempts:
2 left
💡 Hint
Think about how unions share memory and how alignment affects size.
Predict Output
expert
2:00remaining
Output of union with bit fields and overlapping members
What is the output of this program?
C
#include <stdio.h>

union U {
    struct {
        unsigned int a:4;
        unsigned int b:4;
    } bits;
    unsigned char byte;
};

int main() {
    union U u;
    u.byte = 0;
    u.bits.a = 9;  // 1001 in binary
    u.bits.b = 6;  // 0110 in binary
    printf("%u\n", u.byte);
    return 0;
}
ACompilation error
B150
C90
D105
Attempts:
2 left
💡 Hint
Calculate the bits: a in lower 4 bits, b in upper 4 bits.