Switch Case Interview Questions for Embedded C: Quiz and Code Challenges
57 questions: multiple choice, write-the-label blanks, a construct-matching table, true/false, 6 code challenges with real output and 12 detailed interview answers. Treat it as a model for switch case interview questions.
57
Questions
Beginner → 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.
Compiler result:dup.c: In function ‘main’:
dup.c:10:9: error: duplicate case value
10 | case 4 + 1:
| ^~~~
dup.c:7:9: note: previously used here
7 | case 5:
| ^~~~The constant expression 4 + 1 equals 5, so the label duplicates the first one: a compile error.
Challenge 5 — Find the bug: Declaration right after a label
#include <stdio.h>int main(void)
{
int a = 1;
switch (a)
{
case1:
int t = 5;
printf(“%d\n”, t);
break;
case2:
printf(“two\n”);
break;
}
return0;
}
What does GCC say about this switch?
Compiler result:decl.c: In function ‘main’:
decl.c:8:13: warning: a label can only be part of a statement and a declaration is not a statement [-Wpedantic]
8 | int t = 5;
| ^~~Before C23 a label must be followed by a statement. GCC accepts it as an extension in its default mode and warns under -pedantic (C11 mode shown); the portable and safe form is to wrap the case body in braces.
Challenge 6 — Output?: A shift turns a range into a value
7 >> 2 is 1, so the second case runs. The shift maps 0–3 to 0, 4–7 to 1 and everything larger to other values.
Section 6: Switch Case Interview Questions for Embedded C (with Detailed Answers)
Switch case interview questions test your knowledge of fall-through, constants, compilation and state-machine design. Click a question for a model answer.
Q1. What is the difference between switch and if-else?Basic+
A switch compares one integer expression with integer constants and jumps to the matching label; if-else can test any condition, including ranges, floating-point values and several variables. A switch with dense labels can compile to a jump table. The price is the fall-through hazard and the restriction to constants.
Q2. What does default do and is it mandatory?Basic+
Default runs when no case label matches. C does not require it, but MISRA C rule 16.4 does, and good practice uses it to catch unexpected values, for example to log an error or to move a state machine into a safe state.
Q3. What is fall-through? When is it acceptable?Intermediate+
Execution continues from one case into the next when no break stops it. Stacking labels with no code between them is normal. Fall-through with code in between is acceptable only when deliberate, annotated (/* fall through */ or __attribute__((fallthrough))), and reviewed, because it is the most common switch bug.
fallthrough_iq.c
#include <stdio.h>int main(void)
{
int n = 3, total = 0;
switch (n)
{
case3: total += 4; /* fall through */case2: total += 2; /* fall through */case1: total += 1; break;
}
printf(“%d\n”, total);
return0;
}
Output (verified by compiling and running)
7
Q4. How is a switch implemented by the compiler?Intermediate+
Depending on the case values the compiler produces a comparison chain (few cases), a jump table with a bounds check (dense values; on Cortex-M the TBB or TBH instruction), a binary search over comparisons (sparse values) or a lookup table when the cases only produce values. At -O0 the result can differ from -O2, so look at the disassembly when timing matters.
Q5. Can you use a string in a switch? What do you do instead?Intermediate+
No, a switch needs integer expressions. Options: an else-if ladder with strcmp; a table of (string, enum) pairs searched linearly or by binary search and followed by a switch on the enum; a hash of the string computed at run time with compile-time constants only if you have a constant hash. Embedded parsers often switch on the first character and then verify the rest.
Q6. Why put braces around a case that defines variables?Intermediate+
A case label does not create a scope; the variable would be visible in all following cases, and a jump to a later label skips its initialiser, leaving the value indeterminate. Braces limit the scope to that case. Also, before C23 a declaration cannot directly follow a label.
Q7. Write a state machine for a debounced button using a switch.Intermediate+
debounce_fsm_iq.c
#include <stdio.h>typedefenum { UP, MAYBE_DOWN, DOWN, MAYBE_UP } bstate_t;
static bstate_t st = UP;
staticint step(int raw) /* returns the debounced level */
{
switch (st)
{
case UP: if (raw) st = MAYBE_DOWN; break;
case MAYBE_DOWN: st = raw ? DOWN : UP; break;
case DOWN: if (!raw) st = MAYBE_UP; break;
case MAYBE_UP: st = raw ? DOWN : UP; break;
default: st = UP; break;
}
return st == DOWN || st == MAYBE_UP;
}
int main(void)
{
int raw[] = { 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0 };
for (int i = 0; i < 11; i++)
printf(“raw=%d -> %d\n”, raw[i], step(raw[i]));
return0;
}
A change of the raw input must persist for two consecutive samples before the output follows. A single-sample glitch is ignored because the machine returns to its previous stable state.
Q8. When would you replace a switch with a function-pointer table?Advanced+
When there are many cases (dozens of commands), each case calls a function, and the index is dense. A table of function pointers such as handler[cmd]() adds new commands by adding a row, can be stored in flash, and allows unit tests of each handler. Always bounds-check the index and handle NULL entries. Lesson 8.4 covers function pointers.
Q9. A switch on a state variable occasionally does nothing in the field. What could be happening?Advanced+
The state variable probably contains a value that matches no case and the switch has no default: corrupted by a stack overflow, a buffer overrun, an uninitialised variable or concurrent modification from an ISR without volatile or locking. A default that logs and resets the machine turns a silent failure into a diagnosable one. Also check for enum values cast from raw integers.
Q10. Why can switch (c) misbehave for bytes above 127?Advanced+
char may be signed (x86 GCC) or unsigned (ARM GCC). A byte 0xE9 then becomes -23 or 233 and case labels written as character constants may not match as expected. Use uint8_t for data bytes and compare against unsigned constants, and keep char for text.
Q11. What are the MISRA C rules about switch statements?Advanced+
Rule 16.1: switch statements shall be well formed. 16.2: a switch label shall only be used when the most closely enclosing compound statement is the body of a switch (no Duff’s device). 16.3: an unconditional break shall terminate every switch clause. 16.4: every switch shall have a default. 16.5: default shall appear first or last. 16.6: every switch shall have at least two clauses. 16.7: a switch expression shall not have essentially Boolean type. Rules differ slightly between the 2004 and 2012 editions, so check the one your project follows.
Q12. What is Duff’s device and why is it banned in MISRA code?Advanced+
It is an unrolled copy loop that interleaves a switch with a do-while so that the case labels jump into the middle of the loop. It is legal C and saves loop overhead, but it violates the structured-switch rule and is hard to read; modern compilers unroll loops themselves. Know it for interviews; do not use it in production firmware.
Study Plan: Using Your Score on These Switch Case Interview Questions
Score
Meaning
Next step
90–100%
Excellent
Continue to Module 7: loops, starting with the for loop.
70–89%
Good
Redo Examples 3, 5 and 8 of this lesson.
Below 70%
Revise
Re-read theory sections 3, 4 and 6, then retake the quiz.
Habit to build: Compile with -Wall -Wextra and treat every fall-through warning as a bug until you have annotated it.
Module 6 complete!
Next up: Module 7, loops: for, while, do-while, break and continue.
2 Comments