Bird
Raised Fist0
Embedded Cprogramming~20 mins

Struct packing and alignment in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Struct Packing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of struct size with default alignment
What is the output of the following C code when printed with sizeof(s)?
Embedded C
#include <stdio.h>
#include <stddef.h>

struct S {
    char c;
    int i;
};

int main() {
    struct S s;
    printf("%zu", sizeof(s));
    return 0;
}
A12
B5
C8
D16
Attempts:
2 left
💡 Hint
Think about how the compiler aligns the int after the char.
❓ Predict Output
intermediate
2:00remaining
Effect of #pragma pack(1) on struct size
What is the output of the following C code when printed with sizeof(s)?
Embedded C
#include <stdio.h>
#pragma pack(1)
struct S {
    char c;
    int i;
};
#pragma pack()

int main() {
    struct S s;
    printf("%zu", sizeof(s));
    return 0;
}
A16
B8
C12
D5
Attempts:
2 left
💡 Hint
Packing removes padding bytes.
🔧 Debug
advanced
2:30remaining
Why does this struct cause unexpected padding?
Consider this struct and its size output. Why does it have unexpected padding bytes?
Embedded C
struct S {
    char c1;
    double d;
    char c2;
};

// sizeof(S) printed is 24 on a 64-bit system.
ABecause double requires 8-byte alignment, padding is added after c1 and c2.
BBecause the struct is packed with #pragma pack(1).
CBecause the compiler ignores alignment rules for double.
DBecause char variables always cause 8 bytes of padding each.
Attempts:
2 left
💡 Hint
Think about alignment requirements of double and where padding is inserted.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in struct packing directive
Which option contains a syntax error in using #pragma pack to pack a struct?
A
#pragma pack(2)
struct S { char c; int i; };
B
#pragma pack 1
struct S { char c; int i; };
C
#pragma pack(1)
struct S { char c; int i; };
#pragma pack()
D
#pragma pack(push, 1)
struct S { char c; int i; };
#pragma pack(pop)
Attempts:
2 left
💡 Hint
Check the syntax of #pragma directives carefully.
🚀 Application
expert
3:00remaining
Calculate struct size with mixed types and packing
Given the struct below with #pragma pack(1), what is the size of struct S?
Embedded C
#pragma pack(1)
struct S {
    char c;
    short s;
    int i;
    char arr[3];
};
#pragma pack()
A10
B12
C11
D14
Attempts:
2 left
💡 Hint
Add sizes of each member without padding due to packing.

Practice

(1/5)
1. What does struct packing do in embedded C?
easy
A. Removes extra spaces between fields to save memory
B. Adds extra spaces to align data on memory boundaries
C. Changes the data types of struct members
D. Automatically initializes struct members to zero

Solution

  1. Step 1: Understand struct packing purpose

    Struct packing removes padding bytes between fields to reduce memory usage.
  2. Step 2: Compare with alignment

    Alignment adds padding for faster access, packing removes it to save space.
  3. Final Answer:

    Removes extra spaces between fields to save memory -> Option A
  4. Quick Check:

    Struct packing = removes padding [OK]
Hint: Packing removes spaces, alignment adds spaces [OK]
Common Mistakes:
  • Confusing packing with alignment
  • Thinking packing changes data types
  • Assuming packing initializes values
2. Which of the following is the correct syntax to pack a struct in embedded C using GCC?
easy
A. struct MyStruct { int a; char b; } __packed;
B. struct MyStruct __packed { int a; char b; };
C. struct MyStruct { int a; char b; } __attribute__((aligned));
D. struct __attribute__((packed)) MyStruct { int a; char b; };

Solution

  1. Step 1: Recall GCC packing syntax

    GCC uses __attribute__((packed)) after struct keyword or at end of struct definition.
  2. Step 2: Identify correct placement

    struct __attribute__((packed)) MyStruct { int a; char b; }; places __attribute__((packed)) correctly after struct keyword.
  3. Final Answer:

    struct __attribute__((packed)) MyStruct { int a; char b; }; -> Option D
  4. Quick Check:

    GCC packing syntax = __attribute__((packed)) [OK]
Hint: Use __attribute__((packed)) after struct keyword [OK]
Common Mistakes:
  • Using __packed keyword which is not standard GCC
  • Placing __attribute__((aligned)) instead of packed
  • Putting attributes in wrong position
3. Consider this struct without packing:
struct Example {
  char a;
  int b;
};

sizeof(struct Example);

What is the likely output on a 32-bit system where int is 4 bytes and alignment is on 4-byte boundaries?
medium
A. 5
B. 8
C. 12
D. 16

Solution

  1. Step 1: Analyze struct member sizes and alignment

    char a = 1 byte, int b = 4 bytes aligned on 4-byte boundary.
  2. Step 2: Calculate padding and total size

    After char a, 3 bytes padding added to align int b. Total size = 1 + 3 + 4 = 8 bytes. Struct size must be multiple of largest alignment (4), which 8 satisfies.
  3. Final Answer:

    8 -> Option B
  4. Quick Check:

    Size with padding = 8 bytes [OK]
Hint: Add padding after smaller types to align next member [OK]
Common Mistakes:
  • Ignoring padding bytes
  • Adding padding after last member unnecessarily
  • Assuming size is sum of member sizes only
4. Given this code snippet:
struct Data {
  char c;
  int i;
} __attribute__((packed));

struct Data d;
printf("%zu", sizeof(d));

The output is 5, but the program crashes when accessing d.i. What is the likely cause?
medium
A. Accessing unaligned int causes crash on some systems
B. The struct is not actually packed
C. char c is not initialized
D. printf format specifier is incorrect

Solution

  1. Step 1: Understand packing effect on alignment

    Packing removes padding, so int i may not be aligned on 4-byte boundary.
  2. Step 2: Recognize hardware alignment requirements

    Some systems crash or fault when accessing unaligned int variables.
  3. Final Answer:

    Accessing unaligned int causes crash on some systems -> Option A
  4. Quick Check:

    Unaligned access = crash risk [OK]
Hint: Packed structs can cause unaligned access crashes [OK]
Common Mistakes:
  • Blaming printf format specifier
  • Assuming packing always safe
  • Ignoring hardware alignment rules
5. You have a struct with members:
char a;
short b;
int c;

On a 32-bit system, how can you arrange and pack this struct to minimize memory size without causing unaligned access faults?
hard
A. Order members as char a; int c; short b; and use packing
B. Order members as char a; short b; int c; without packing
C. Order members as int c; short b; char a; and use packing
D. Order members as short b; char a; int c; without packing

Solution

  1. Step 1: Understand alignment requirements

    int requires 4-byte alignment, short 2-byte, char 1-byte.
  2. Step 2: Arrange members largest to smallest

    Ordering int c; short b; char a; places largest first, minimizing padding.
  3. Step 3: Use packing carefully

    Packing removes padding but can cause unaligned access; here, ordering avoids unaligned access.
  4. Final Answer:

    Order members as int c; short b; char a; and use packing -> Option C
  5. Quick Check:

    Largest to smallest + packing = minimal size safe [OK]
Hint: Order largest to smallest type, then pack [OK]
Common Mistakes:
  • Ordering members smallest to largest causing padding
  • Packing without considering alignment faults
  • Ignoring hardware alignment rules
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard