Endianness and Alignment Interview Questions for Embedded C: Quiz and Code Challenges
57 questions: multiple choice, write-the-expression blanks, a tool-matching table, true/false, 6 code challenges with real output and 12 detailed interview answers. Treat it as a model for endianness and alignment interview questions.
57
Questions
Intermediate → Advanced
Level
Live score
Auto-graded
Live Score Tracker
0 Correct
0 Wrong
0 Attempted
Answer the auto-graded questions (MCQ, fill-in, true/false) to see your score.
Section 1: Multiple Choice Questions — Casting, Alignment and Byte Order
These endianness and alignment interview questions appear in nearly every embedded C screening round. Click an answer; explanations always appear.
1. Which byte of 0x12345678 is stored at the lowest address on the Cortex-M4?
Little-endian: least significant byte first.
2. What does a pointer cast like (uint16_t *)p change?
Nothing is converted.
3. The natural alignment of a uint32_t is:
Equal to its size.
4. What is the size of struct { uint8_t a; uint32_t b; } with default padding?
3 padding bytes after a.
5. Which Cortex-M4 instruction raises a UsageFault on an unaligned address?
Multi-word accesses must be aligned; single LDR/STR may be unaligned.
6. Which pointer type may legally examine the bytes of any object?
Character types are exempt from strict aliasing.
7. What is the safest way to reinterpret a float as a uint32_t?
memcpy is always legal.
8. What does (uint32_t)3.9f produce?
An arithmetic cast converts the value, truncating.
9. Which instruction reverses the bytes of a register on ARM?
CMSIS: __REV().
10. Network byte order is:
Most significant byte first.
11. Which expression reads a big-endian 16-bit value from uint8_t *p?
High byte first.
12. What type should hold an address converted to an integer?
Guaranteed to round-trip a pointer.
13. What does __attribute__((packed)) do to a structure?
Members may become unaligned.
14. Why does GCC warn when you take the address of a packed member?
-Waddress-of-packed-member.
15. Which statement about type punning through a union is true in C99/C11?
But the result depends on endianness.
Section 2: Write or Evaluate (Fill in the Blanks)
Type the expression or statement for the first three items (spacing is flexible) and a short value for the rest.
1. Write the expression that reads a big-endian 16-bit value from uint8_t *p:
The first byte is the high byte.
2. Write the test that is true when pointer p is 4-byte aligned (use uintptr_t):
The low two bits must be zero.
3. Write the call that copies the 4 bytes of float f into uint32_t u safely:
memcpy avoids aliasing violations.
4. 0x12345678 byte-swapped is 0x
Reverse the four bytes.
5. sizeof(struct {uint8_t a; uint32_t b; uint8_t c;}) is
Padding after a and c.
6. The byte at the lowest address of 0xAABBCCDD (little-endian) is 0x
Least significant byte.
7. A 12-bit field 0x800 sign-extended is
Bit 11 is the sign.
8. The bit pattern of the float 1.0 is 0x
IEEE-754 single precision.
Section 3: Which Tool Fits?
Choose the tool for each task, as asked in endianness and alignment interview questions.
Task
Your answer
Result
Reinterpret float bits safely
memcpyuint8_t *uintptr_t__REV_Alignas_Alignof__attribute__((packed))shifts and masks
Examine any object byte by byte
memcpyuint8_t *uintptr_t__REV_Alignas_Alignof__attribute__((packed))shifts and masks
Hold a pointer value as an integer
memcpyuint8_t *uintptr_t__REV_Alignas_Alignof__attribute__((packed))shifts and masks
Reverse the bytes of a 32-bit register
memcpyuint8_t *uintptr_t__REV_Alignas_Alignof__attribute__((packed))shifts and masks
Fix object alignment in C11
memcpyuint8_t *uintptr_t__REV_Alignas_Alignof__attribute__((packed))shifts and masks
Query the alignment of a type
memcpyuint8_t *uintptr_t__REV_Alignas_Alignof__attribute__((packed))shifts and masks
Remove structure padding
memcpyuint8_t *uintptr_t__REV_Alignas_Alignof__attribute__((packed))shifts and masks
Read a field independent of host byte order
memcpyuint8_t *uintptr_t__REV_Alignas_Alignof__attribute__((packed))shifts and masks
Section 4: True or False
Statements typical of endianness and alignment interview questions.
1. The Cortex-M4 is big-endian.
It is little-endian.
2. A cast between pointer types converts the pointed-to data.
It only reinterprets.
3. Reading a float through a uint32_t * violates strict aliasing.
Use memcpy or a union.
4. The Cortex-M4 never faults on unaligned accesses.
LDM, LDRD and VLDR fault.
5. A packed struct fixes the byte order of its members.
It only removes padding.
6. Assembling an integer from bytes with shifts is independent of the CPU byte order.
The wire order is stated explicitly.
7. Padding bytes can be sent safely over a protocol by writing the whole struct.
They contain uninitialised data and depend on the compiler.
8. uint8_t * can alias any object.
As a character type.
Section 5: Code Snippet Challenges
Trace each program on paper first; the real output follows. These challenges are the code-reading half of endianness and alignment interview questions.
a at 0, one padding byte, b at 2, c at 4, one trailing pad byte so that arrays keep b aligned: size 6, alignment 2.
Challenge 4 — Output?: Conversions
#include <stdio.h>#include <stdint.h>int main(void)
{
int8_t a = (int8_t)200;
uint8_t b = (uint8_t)-1;
int c = (int)3.99;
int d = (int)-3.99;
printf(“%d %u %d %d\n”, a, b, c, d);
return0;
}
What is printed (GCC)?
Output:
-56 255 3 -3
200 wraps to −56 for int8_t (implementation-defined); −1 as uint8_t is 255 (defined); float to int truncates towards zero: 3 and −3.
Challenge 5 — Find the bug: Type punning with a pointer cast
The two bytes are exchanged: 0xABCD becomes 0xCDAB.
Section 6: Endianness and Alignment Interview Questions for Embedded C (with Detailed Answers)
Endianness and alignment interview questions test byte order, padding, strict aliasing, unaligned access and safe serialisation. Click a question for a model answer.
Q1. What is endianness?Basic+
The order in which the bytes of a multi-byte value are stored. Little-endian stores the least significant byte at the lowest address (ARM Cortex-M, x86); big-endian stores the most significant byte first (network protocols, some other CPUs). It matters when exchanging binary data between systems, in memory dumps and when overlaying types.
Q2. How do you determine the endianness of the CPU at run time and at compile time?Basic+
Look at the first byte of the integer 1 through a byte pointer, or test the compiler macro __BYTE_ORDER__ at compile time. The compile-time form lets the compiler remove the unused branch.
Q3. What is alignment? Why does it matter?Basic+
Alignment is the requirement that an object of size N be placed at an address that is a multiple of N (up to the maximum alignment). Hardware performs aligned accesses in one bus transfer; unaligned accesses may be slower, split into several transfers, or raise a fault depending on the instruction and the core. DMA engines and some instructions (LDM, LDRD, VLDR, LDREX) require alignment.
Q4. What is structure padding and how can you reduce it?Intermediate+
Order of members
sizeof
uint8_t a; uint32_t b; uint8_t c;
12
uint32_t b; uint8_t a; uint8_t c;
8
same with packed
6
The compiler inserts padding to keep each member naturally aligned and the total size a multiple of the largest alignment. Sort members by decreasing size to minimise padding. Packing removes it, but creates unaligned members that cost cycles or fault in multi-word instructions.
Q5. Explain the strict aliasing rule.Intermediate+
An object may be accessed only through an lvalue of its own type, a compatible type, a signed/unsigned variant, an aggregate containing it, or a character type. Accessing a float as uint32_t through a cast pointer is undefined, so the optimiser may reorder or drop the access. Fixes: memcpy, a union, or reading bytes through uint8_t *. The flag -fno-strict-aliasing disables the assumption (the Linux kernel uses it) at some performance cost.
Q6. How do you safely read a 32-bit big-endian integer from a byte buffer?Intermediate+
Assemble it from bytes with shifts. Cast each byte to uint32_t before shifting (a uint8_t promotes to int, and shifting a value into bit 31 of a signed int is undefined). The function works at any alignment and any host byte order.
Q7. Why is *(uint32_t *)&buf[1] wrong?Intermediate+
Three reasons: (1) misaligned address — the Cortex-M4 may fault for LDM/LDRD that the compiler may generate, and the Cortex-M0 always faults; (2) strict aliasing violation if buf is not a character array of the right effective type; (3) the result has the host byte order, not necessarily the protocol’s. Use shifts or memcpy plus an explicit swap.
Q8. How does the Cortex-M4 handle unaligned accesses?Advanced+
Single LDR/LDRH/STR/STRH (and their signed variants) to unaligned addresses are supported in hardware on Cortex-M3/M4 (slower, split into multiple bus transfers) unless the UNALIGN_TRP bit of the CCR register is set, which makes them fault. LDM/STM, LDRD/STRD, PUSH/POP of multiple registers, LDREX/STREX and VLDR/VSTR require alignment and raise a UsageFault. The Cortex-M0/M0+ faults on all unaligned word and half-word accesses. Normal compiler output for aligned pointers is safe on all.
Q9. What are the dangers of __attribute__((packed))?Advanced+
Members become unaligned, so access may be slower or fault if the address of a member is taken and used as an ordinary pointer. Because the compiler is aware of the packing, direct member access generates safe code (byte loads or unaligned-tolerant instructions), but a pointer to the member loses that information (-Waddress-of-packed-member). A packed layout also does not define byte order and may differ between compilers; define wire formats with explicit serialisation.
Q10. How do you align a buffer for DMA?Advanced+
Use the language: _Alignas(4) uint8_t buf[N]; or the GCC attribute __attribute__((aligned(4))) (or 32 for cache line alignment on cores with data caches). The DMA data width (byte, half-word, word) must match the alignment of both the memory address and the peripheral register. On cores with caches (Cortex-M7) the buffer should also be cache-line aligned and sized, which does not apply to the Cortex-M4.
Q11. What is the difference between (int)f and reading a float’s bits?Advanced+
(int)f is an arithmetic conversion: it computes the value of f truncated toward zero (3.9f becomes 3). Reading the bits (via memcpy or a union) gives the IEEE-754 representation, for example 0x3F800000 for 1.0f. They are different operations; a pointer cast followed by dereference tries the second but is undefined behavior under strict aliasing.
Q12. A structure is sent from an STM32 (little-endian) to a big-endian host with HAL_UART_Transmit(&huart, (uint8_t *)&s, sizeof s, ...). What goes wrong?Advanced+
Multi-byte members arrive with the wrong byte order; padding bytes (with indeterminate content) are transmitted; layout differences between compilers shift the offsets; and the receiver may have different member sizes (for example long). Define an explicit wire format: fixed-width types, fixed byte order, and serialise field by field with shifts; verify with a length and a CRC.
Study Plan: Using Your Score on These Endianness and Alignment Interview Questions
Score
Meaning
Next step
90–100%
Excellent
Continue to Lesson 9.6: memory-mapped register access with pointers.
70–89%
Good
Redo Examples 2, 4 and 5 of the examples page.
Below 70%
Revise
Re-read theory sections 2, 3, 4 and 5, then retake the quiz.
Habit to build: For every cast, write one comment line: what is the real type at this address, and is it aligned?
Lesson 9.5 complete!
Next up: memory-mapped register access with pointers: base addresses, register structs, volatile and bit manipulation.
2 Comments