0
0
Cprogramming~20 mins

Structure vs union comparison - Practice Questions

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

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

int main() {
    struct Data s = {5, 'A', 3.14};
    printf("%d %c %.2f\n", s.i, s.c, s.d);
    printf("Size of struct Data: %zu\n", sizeof(s));
    return 0;
}
A
5 A 3.14
Size of struct Data: 12
B
5 A 3.14
Size of struct Data: 24
C
5 A 3.14
Size of struct Data: 16
D
5 A 3.14
Size of struct Data: 13
Attempts:
2 left
💡 Hint
Think about padding and alignment in structures.
Predict Output
intermediate
2:00remaining
Output of union member assignment
What will be printed by this C program?
C
#include <stdio.h>

union Value {
    int i;
    float f;
};

int main() {
    union Value v;
    v.i = 1065353216;
    printf("%d %.2f\n", v.i, v.f);
    return 0;
}
A1065353216 1.00
B1065353216 0.00
C0 1.00
D1065353216 1065353216.00
Attempts:
2 left
💡 Hint
Remember that union members share the same memory.
🔧 Debug
advanced
2:30remaining
Why does this union code print unexpected values?
Consider this code snippet. Why does printing both union members after assignment give unexpected results?
C
#include <stdio.h>

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

int main() {
    union Data d;
    d.i = 0x41424344;
    printf("%d %c %c %c %c\n", d.i, d.str[0], d.str[1], d.str[2], d.str[3]);
    return 0;
}
ABecause the char array is not initialized, it contains garbage values unrelated to the int.
BBecause the union stores all members in the same memory, the char array shows the byte representation of the int, which depends on endianness.
CBecause the int and char array have different sizes, accessing str causes a buffer overflow.
DBecause the printf format specifiers are incorrect for union members.
Attempts:
2 left
💡 Hint
Think about how data is stored in memory and how endianness affects byte order.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly declares and initializes a union?
Which of the following code snippets correctly declares a union and initializes its member?
Aunion Number n; n.i = 10; union Number { int i; float f; };
Bunion Number { int i; float f; } n; n = {10};
Cunion Number { int i; float f; } n = (int)10;
Dunion Number { int i; float f; } n = { .i = 10 };
Attempts:
2 left
💡 Hint
Look for correct syntax for union declaration and initialization.
🚀 Application
expert
3:00remaining
Memory size comparison between struct and union
Given the following definitions, what is the total size in bytes of struct S and union U on a typical 64-bit system?
C
struct S {
    char c;
    int i;
    double d;
};

union U {
    char c;
    int i;
    double d;
};
Astruct S: 16 bytes, union U: 8 bytes
Bstruct S: 16 bytes, union U: 24 bytes
Cstruct S: 24 bytes, union U: 8 bytes
Dstruct S: 24 bytes, union U: 24 bytes
Attempts:
2 left
💡 Hint
Remember struct size includes all members plus padding; union size equals largest member size.