EmbedwrightArticlesLesson guideFree lessonsThe course · $79

Firmware and embedded C interview questions, with answers

By Chinmay Gupta · Updated 2026-10-10
Short answer. Expect questions on volatile, const volatile, endianness, bit manipulation, what an ISR may and may not do, stack versus heap, and priority inversion. The best answers are short, name the exact rule, and mention the exception or the platform dependency.

These are the questions that come up again and again in embedded and firmware interviews. The answers are written the way a strong candidate would say them: short, specific, and honest about what depends on the compiler or the hardware.

1. What does volatile do, and when do you need it?

It tells the compiler that every access to the object is observable, so it must not cache the value in a register or remove accesses. You need it for memory-mapped registers and for variables shared with an interrupt handler. It does not make accesses atomic and does not order them against non-volatile memory. Full detail: what volatile does.

2. Can a variable be both const and volatile?

Yes. A read-only hardware status register is const volatile: your code must not write it, but its value can change at any time.

3. How do you detect endianness at run time?

Copy the bytes of a known value and look at the first one. Using memcpy avoids type-punning problems:

#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>

static bool is_little_endian(void)
{
    uint32_t v = 1u;
    uint8_t b[4];
    memcpy(b, &v, sizeof v);
    return b[0] == 1u;
}

static uint32_t bswap32(uint32_t x)
{
    return (x >> 24) | ((x >> 8) & 0x0000FF00u) | ((x << 8) & 0x00FF0000u) | (x << 24);
}

static bool is_pow2(uint32_t x) { return x != 0u && (x & (x - 1u)) == 0u; }

#define SET_BIT(r, n)    ((r) |= (1u << (n)))
#define CLEAR_BIT(r, n)  ((r) &= ~(1u << (n)))
#define TOGGLE_BIT(r, n) ((r) ^= (1u << (n)))

int main(void)
{
    printf("little endian: %s\n", is_little_endian() ? "yes" : "no");
    printf("bswap32(0x11223344) = 0x%08X\n", (unsigned)bswap32(0x11223344u));
    printf("is_pow2(1024)=%d is_pow2(1000)=%d is_pow2(0)=%d\n", is_pow2(1024), is_pow2(1000), is_pow2(0));
    uint32_t reg = 0;
    SET_BIT(reg, 3); SET_BIT(reg, 7); CLEAR_BIT(reg, 3); TOGGLE_BIT(reg, 0);
    printf("reg = 0x%02X (expect 0x81)\n", (unsigned)reg);
    return (bswap32(0x11223344u) == 0x44332211u && reg == 0x81u) ? 0 : 1;
}
Real output
little endian: yes
bswap32(0x11223344) = 0x44332211
is_pow2(1024)=1 is_pow2(1000)=0 is_pow2(0)=0
reg = 0x81 (expect 0x81)

Compiled with -Wall -Wextra -Werror and run under AddressSanitizer + UBSan.

On a compiler you can rely on, the byte-swap helper is often a single REV instruction on Arm. The 1u literal in the macros matters: 1 << 31 on a signed int is a bug.

4. How do you set, clear and toggle a bit without disturbing the others?

Set with OR, clear with AND of the inverted mask, toggle with XOR, as in the macros above. Mention the unsigned literal, and that on a volatile register each of these is a read-modify-write, which is not atomic against an interrupt that touches the same register.

5. What can an interrupt service routine do, and what should it not do?

Keep it short and non-blocking. It should not call blocking functions, use malloc, call non-reentrant library functions such as printf, or hold locks for long. The usual pattern is to read the hardware, put data in a buffer such as a ring buffer, set a flag, and return, with the processing done in the main loop or a task.

Learning this for work or an interview? Lessons 1 and 2 of DSA for Firmware are free, with labs and C exercises. Start Lesson 1

6. Stack versus heap in an embedded system?

Stack memory is automatic, fast and bounded by the stack size you configure; overflowing it corrupts whatever sits below it, and in practice you must measure the high-water mark. The heap allows dynamic lifetimes but can fragment, has non-deterministic timing, and can fail at run time, so many embedded standards (and MISRA C) restrict or forbid it. Prefer static allocation or fixed-size pools.

7. What is priority inversion and how do you prevent it?

A high-priority task waits on a lock held by a low-priority task that a medium-priority task is preempting. Use priority inheritance or ceiling mutexes, keep critical sections short, and avoid sharing locks across very different priorities. See priority inversion explained for a simulation.

8. What is the difference between a macro and an inline function?

A macro is text substitution before compilation: no type checking, arguments may be evaluated more than once, and it is hard to debug. A static inline function is type-checked and evaluated once per argument, and the compiler can still inline it. Prefer inline functions; use macros for constants, token pasting and things a function cannot express.

9. Why is sizeof(struct) larger than the sum of its members?

Padding, inserted so each member is aligned, plus tail padding so arrays of the struct stay aligned. Reorder members by size to save RAM, and never rely on layout for a wire format. See struct padding and alignment.

10. How would you store configuration so a power cut cannot corrupt it?

Two slots, a sequence number and a CRC, written so the new record only becomes valid at the very last write. See power-fail-safe storage in flash.

How to answer well

State the rule, then the exception, then how you would verify it. For example: "volatile forces the access, but it is not atomic, so for a counter shared with an ISR I would also guard the update." That pattern, rule plus limit plus check, is what separates a memorized answer from an engineer's answer.

For a longer plan of which data structures to practice, see what to learn and in what order.

Want the full path from “I know C” to “I can choose the right data structure under a RAM budget”?
DSA for Firmware teaches it one real bug at a time.
  • 28 lessons, each starting from a real firmware bug.
  • 110 C exercises with tests, and a reference solution for every one.
  • Lessons 1 and 2 are free. No sign-up.
Start Lesson 1 freeSee the full course · $79

Get the next article by email

One short email when a new article or free lesson is published. No spam. Unsubscribe at any time by replying to any email. See the privacy policy.

Chinmay Gupta
Chinmay Gupta is a firmware engineer with more than eight years of experience writing and debugging embedded C. He builds Embedwright's DSA for Firmware course.