Endianness and Alignment Interview Questions for Embedded C: Quiz and Code Challenges-Free Embedded C Course in Hyderabad

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

MODULE 9 · LESSON 9.5 · PRACTICE

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?

2. What does a pointer cast like (uint16_t *)p change?

3. The natural alignment of a uint32_t is:

4. What is the size of struct { uint8_t a; uint32_t b; } with default padding?

5. Which Cortex-M4 instruction raises a UsageFault on an unaligned address?

6. Which pointer type may legally examine the bytes of any object?

7. What is the safest way to reinterpret a float as a uint32_t?

8. What does (uint32_t)3.9f produce?

9. Which instruction reverses the bytes of a register on ARM?

10. Network byte order is:

11. Which expression reads a big-endian 16-bit value from uint8_t *p?

12. What type should hold an address converted to an integer?

13. What does __attribute__((packed)) do to a structure?

14. Why does GCC warn when you take the address of a packed member?

15. Which statement about type punning through a union is true in C99/C11?

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:

2. Write the test that is true when pointer p is 4-byte aligned (use uintptr_t):

3. Write the call that copies the 4 bytes of float f into uint32_t u safely:

4. 0x12345678 byte-swapped is 0x

5. sizeof(struct {uint8_t a; uint32_t b; uint8_t c;}) is

6. The byte at the lowest address of 0xAABBCCDD (little-endian) is 0x

7. A 12-bit field 0x800 sign-extended is

8. The bit pattern of the float 1.0 is 0x

Section 3: Which Tool Fits?

Choose the tool for each task, as asked in endianness and alignment interview questions.

TaskYour answerResult
Reinterpret float bits safelymemcpyuint8_t *uintptr_t__REV_Alignas_Alignof__attribute__((packed))shifts and masks
Examine any object byte by bytememcpyuint8_t *uintptr_t__REV_Alignas_Alignof__attribute__((packed))shifts and masks
Hold a pointer value as an integermemcpyuint8_t *uintptr_t__REV_Alignas_Alignof__attribute__((packed))shifts and masks
Reverse the bytes of a 32-bit registermemcpyuint8_t *uintptr_t__REV_Alignas_Alignof__attribute__((packed))shifts and masks
Fix object alignment in C11memcpyuint8_t *uintptr_t__REV_Alignas_Alignof__attribute__((packed))shifts and masks
Query the alignment of a typememcpyuint8_t *uintptr_t__REV_Alignas_Alignof__attribute__((packed))shifts and masks
Remove structure paddingmemcpyuint8_t *uintptr_t__REV_Alignas_Alignof__attribute__((packed))shifts and masks
Read a field independent of host byte ordermemcpyuint8_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.

2. A cast between pointer types converts the pointed-to data.

3. Reading a float through a uint32_t * violates strict aliasing.

4. The Cortex-M4 never faults on unaligned accesses.

5. A packed struct fixes the byte order of its members.

6. Assembling an integer from bytes with shifts is independent of the CPU byte order.

7. Padding bytes can be sent safely over a protocol by writing the whole struct.

8. uint8_t * can alias any object.

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.

Challenge 1 — Output?: Bytes of a word
#include <stdio.h> #include <stdint.h> int main(void) { uint32_t v = 0x01020304u; uint8_t *p = (uint8_t *)&v; printf(“%u %u\n”, p[0], p[3]); return 0; }

What is printed on a little-endian machine?

Output:
4 1
The least significant byte 04 is at the lowest address, the most significant 01 at the highest.
Challenge 2 — Output?: Two byte orders from one array
#include <stdio.h> #include <stdint.h> int main(void) { uint8_t b[4] = {0x12, 0x34, 0x56, 0x78}; uint32_t be = ((uint32_t)b[0] << 24) | ((uint32_t)b[1] << 16) | ((uint32_t)b[2] << 8) | b[3]; uint32_t le = ((uint32_t)b[3] << 24) | ((uint32_t)b[2] << 16) | ((uint32_t)b[1] << 8) | b[0]; printf(“%08X %08X\n”, (unsigned)be, (unsigned)le); return 0; }

What is printed?

Output:
12345678 78563412
Reading the bytes first-to-last as big-endian gives 12345678; as little-endian 78563412.
Challenge 3 — Output?: Padding
#include <stdio.h> #include <stdint.h> #include <stddef.h> struct S { uint8_t a; uint16_t b; uint8_t c; }; int main(void) { printf(“%zu %zu %zu %zu\n”, sizeof(struct S), offsetof(struct S, b), offsetof(struct S, c), _Alignof(struct S)); return 0; }

What is printed?

Output:
6 2 4 2
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); return 0; }

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
#include <stdint.h> uint32_t bits(float f) { return *(uint32_t *)&f; } int main(void) { return (int)bits(1.0f); }

What does GCC report with -O2?

Compiler result:punning.c: In function ‘bits’: punning.c:4:13: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] 4 | return *(uint32_t *)&f; | ^~~~~~~~~~~~~~Use memcpy into a uint32_t instead.
Challenge 6 — Output?: swap16
#include <stdio.h> #include <stdint.h> int main(void) { uint16_t x = 0xABCD; uint16_t y = (uint16_t)((x << 8) | (x >> 8)); printf(“%04X\n”, y); return 0; }

What is printed?

Output:
CDAB
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+
endian_iq.c
#include <stdio.h> #include <stdint.h> int main(void) { uint16_t v = 1; printf(“%s\n”, *(uint8_t *)&v ? “little” : “big”); #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ printf(“compile time: little\n”); #endif return 0; }
Output (verified by compiling and running)
little compile time: little

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 memberssizeof
uint8_t a; uint32_t b; uint8_t c;12
uint32_t b; uint8_t a; uint8_t c;8
same with packed6

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+
be32_iq.c
#include <stdio.h> #include <stdint.h> static uint32_t rd_be32(const uint8_t *p) { return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | p[3]; } int main(void) { uint8_t buf[5] = { 0xFF, 0x00, 0x01, 0x86, 0xA0 }; printf(“%lu\n”, (unsigned long)rd_be32(buf + 1)); return 0; }
Output (verified by compiling and running)
100000

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

ScoreMeaningNext step
90–100%ExcellentContinue to Lesson 9.6: memory-mapped register access with pointers.
70–89%GoodRedo Examples 2, 4 and 5 of the examples page.
Below 70%ReviseRe-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.

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *