If Else Interview Questions for Embedded C: Quiz and Code Challenges
58 questions: multiple choice, write-the-condition blanks, a construct-matching table, true/false, 6 code challenges with real output and 12 detailed interview answers. Treat it as a model for if else interview questions.
58
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.
Section 1: Multiple Choice Questions — if, else and Conditions
These if else interview questions in multiple-choice form are common in written screening tests. Click an answer; explanations always appear.
1. Which values are treated as false in an if condition?
Zero of any scalar type is false; every non-zero value is true.
2. What does if (x = 5) do?
The assignment yields 5, which is non-zero.
3. What happens with if (x > 3); followed by a printf line?
The semicolon is an empty body.
4. In if (a) if (b) X; else Y; the else belongs to:
An else pairs with the nearest unmatched if.
5. In an else-if ladder, how many bodies can run?
At most one; with a final else exactly one.
6. Which is the correct range check for 10 <= v <= 20?
Chained comparisons do not work in C.
7. What is the value of the condition 5 > 3 in C?
Relational operators give int 1 or 0.
8. Which condition is safe when p may be NULL?
The NULL test must be evaluated first so that && skips the dereference.
9. Which of these is true for the value '0' (the character)?
Only the NUL character with value 0 is false.
10. What is a guard clause?
It reduces nesting.
11. Why is hysteresis used with a threshold?
Different switch-on and switch-off levels.
12. Which floating-point comparison is the right style?
Rounding errors make exact equality unreliable.
13. What does MISRA C require at the end of an else-if ladder?
Rule 15.7: every if…else if ends with an else.
14. A flag is set in an ISR and tested in if (flag) in main. What is needed?
Otherwise the compiler may keep the value in a register.
15. For input 50 which branch runs? if (p > 50) A; else if (p >= 50) B; else C;
50 > 50 is false; 50 >= 50 is true.
16. Which statement about braces is correct?
They make the body a compound statement and avoid dangling-else and added-line bugs.
Section 2: Write or Evaluate (Fill in the Blanks)
Type the code for the first three items (spacing is flexible) and a short value for the rest.
1. Write an if-else test that sets led = 1 when temp is above 75 and otherwise sets led = 0 (one line, no braces):
if (temp > 75) led = 1; else led = 0;
2. Write the condition (only the expression, no if) that tests bit 0 of IDR:
IDR & 1u
3. Write the condition that is true when v is between 5 and 9 inclusive:
v >= 5 && v <= 9
4. int x = 4; if (x > 3) x += 2; else x -= 2; leaves x =
The condition is true.
5. int x = 0; if (x = 7) x++; leaves x =
The assignment gives 7, which is true, then x++ gives 8.
6. if (-1) is (true or false)
Any non-zero value is true.
7. The number of bodies that can run in an else-if ladder with a final else is exactly
Exactly one.
8. int a = 2, b = 3; int m = (a > b) ? a : b; gives m =
a > b is false.
Section 3: Which Construct Fits?
Match the situation with the best construct, as in many if else interview questions about design.
Situation
Your answer
Result
Choose one of four LED actions from a single character
Statements typical of if else interview questions.
1. The condition of an if statement must be of type bool.
Any scalar type is accepted.
2. else always pairs with the nearest preceding unmatched if.
Indentation is ignored.
3. In if (a && f()) the function f is called even if a is false.
Short-circuit evaluation skips it.
4. An else-if ladder can run two of its bodies.
The first true test wins and the rest are skipped.
5. if (x == 5) and if (5 == x) behave identically.
The second form just makes the typo 5 = x a compile error.
6. A NULL pointer is false in a condition.
NULL compares equal to zero.
7. if (a < b < c) tests whether b lies between a and c.
It tests (a < b) < c.
8. Using braces changes how the code is compiled for a one-line body.
The generated code is the same; braces only protect future edits.
Section 5: Code Snippet Challenges
Trace each program on paper first; the real output follows. These challenges are the code-reading half of if else interview questions.
Challenge 1 — Output?: Stray semicolon and assignment
#include <stdio.h>int main(void)
{
int x = 3;
if (x > 5);
printf(“A\n”);
if (x = 0)
printf(“B\n”);
else
printf(“C\n”);
printf(“x=%d\n”, x);
return0;
}
What is printed, and what warnings would GCC give?
Output:
A
C
x=0
The semicolon makes the first body empty, so A is always printed. In the second statement x is assigned 0, which is false, so C is printed and x becomes 0. -Wall warns about both.
Challenge 2 — Output?: Which if owns the else?
#include <stdio.h>int main(void)
{
int a = 1, b = 2;
if (a == 1)
if (b == 1)
printf(“P\n”);
else
printf(“Q\n”);
else
printf(“R\n”);
return0;
}
What is printed?
Output:
Q
The inner else pairs with if (b == 1). a == 1 is true, b == 1 is false, so Q is printed. R would run only if a were not 1.
Challenge 3 — Output?: if followed by an else-if ladder
#include <stdio.h>int main(void)
{
int v = 7;
if (v < 10) printf(“small “);
if (v < 5) printf(“tiny “);
elseif (v < 8) printf(“medium “);
else printf(“large “);
printf(“\n”);
return0;
}
What is printed?
Output:
small medium
The first if is a separate statement and prints “small”. The ladder that follows starts at the second if: 7 < 5 is false, 7 < 8 is true, so “medium” is printed.
Challenge 4 — Output?: The chained comparison
#include <stdio.h>int main(void)
{
int v = 150;
if (10 <= v <= 100)
printf(“in range\n”);
else
printf(“out of range\n”);
return0;
}
What is printed and why?
Output:
in range
10 <= 150 is 1, and 1 <= 100 is true, so the wrong branch runs. Use v >= 10 && v <= 100.
Challenge 5 — Output?: Short-circuit evaluation
#include <stdio.h>staticint f(int x) { printf(“f(%d) “, x); return x; }
int main(void)
{
if (f(0) && f(1)) printf(“both\n”);
else printf(“no\n”);
if (f(1) || f(2)) printf(“either\n”);
return0;
}
Which calls of f happen?
Output:
f(0) no
f(1) either
In the first condition f(0) is false, so f(1) is skipped. In the second f(1) is true, so f(2) is skipped.
Challenge 6 — Output?: Equality of floating-point values
#include <stdio.h>int main(void)
{
float a = 0.1f + 0.2f;
if (a == 0.3f) printf(“equal\n”);
else printf(“not equal\n”);
double d = 0.1 + 0.2;
if (d == 0.3) printf(“equal\n”);
else printf(“not equal\n”);
return0;
}
What do the two comparisons print on this PC?
Output:
equal
not equal
The float and double results differ because of rounding: with this compiler the rounded float sum happens to equal the float nearest 0.3, while the double sum is slightly above 0.3. Never rely on exact equality; compare with a tolerance.
Section 6: If Else Interview Questions for Embedded C (with Detailed Answers)
If else interview questions check that you can reason about control flow, side effects and robustness, not only syntax. Click a question for a model answer.
Q1. What is considered true and false in C?Basic+
Zero is false and everything else is true. This includes integers, floating-point values and pointers (NULL is false). The relational and logical operators yield an int that is exactly 1 or 0, but an if statement accepts any scalar expression.
Q2. What is the difference between several if statements and an else-if ladder?Basic+
Separate ifs are independent: every condition is evaluated and several bodies can run. In a ladder the conditions are tested in order, the first true one runs its body, and the rest are skipped, so at most one body runs (exactly one with a final else). Use a ladder when the cases are mutually exclusive.
ifs_vs_ladder_iq.c
#include <stdio.h>int main(void)
{
int x = 15;
if (x > 10) printf(“A “);
if (x > 5) printf(“B “);
printf(“| “);
if (x > 10) printf(“A “);
elseif (x > 5) printf(“B “);
printf(“\n”);
return0;
}
Output (verified by compiling and running)
A B | A
Q3. Explain the dangling else.Intermediate+
When one if has no else and is nested directly inside another if that is followed by an else, the grammar allows two pairings. C resolves it by attaching the else to the nearest preceding if without an else. Misleading indentation does not change that. Braces around every body remove the problem; GCC reports it with -Wdangling-else.
Q4. Why do coding standards require braces around single statements?Intermediate+
A later edit that adds a second statement under an unbraced if does not put it inside the if (the goto fail bug in a 2014 TLS library is the best-known example). Braces also remove the dangling else, and they make diffs smaller. The compiled code is identical.
Q5. How would you restructure code with five levels of nested ifs?Intermediate+
Use guard clauses: test each failure condition first, and return (or goto cleanup in C resource handling) immediately. The remaining code is the main path at one indentation level. Alternatively move inner levels into small functions with names that describe the check. Both reduce cyclomatic complexity, which tools such as the MISRA checkers limit to about 10 per function.
Q6. Why write if (5 == x)? What are the drawbacks?Intermediate+
If you mistype =, 5 = x does not compile, whereas x = 5 would silently assign. The drawback is readability: it reads unnaturally. Modern compilers with -Wall warn about assignments in conditions, so many teams prefer the natural order plus warnings as errors (-Werror).
Q7. What is short-circuit evaluation and how can it cause bugs?Intermediate+
For && the right operand is evaluated only if the left is true; for || only if the left is false. It makes p != NULL && *p == 3 safe. It causes bugs when the right operand has a required side effect, for example if (ok && (count++ > 3)), or a function that must always run such as if (check_a() && check_b()) when both must execute. Call the functions first, then test the results.
Q8. How do you compare floating-point numbers in a condition?Intermediate+
Not with ==, because decimal fractions like 0.1 cannot be represented exactly. Test the absolute difference against a tolerance chosen for the application: fabsf(a - b) < 1e-5f, or a relative tolerance for large values. On Cortex-M4 with the single-precision FPU use the f variants to avoid slow double-precision software.
float_cmp_iq.c
#include <stdio.h>#include <math.h>int main(void)
{
double a = 0.1 + 0.2, b = 0.3;
printf(“exact: %d tolerance: %d\n”, a == b, fabs(a – b) < 1e-9);
return0;
}
Output (verified by compiling and running)
exact: 0 tolerance: 1
Q9. Write the safest way to test a flag set in an interrupt.Advanced+
Declare the flag volatile so each test reads memory. If the main loop must also clear it, the read-test-clear sequence must not lose an event set between the test and the clear: disable interrupts around it, or use an atomic exchange, or use a counter that the ISR increments and main compares against its own copy.
Q10. What is cyclomatic complexity and how does if-else affect it?Advanced+
Cyclomatic complexity is the number of linearly independent paths through a function, roughly 1 plus the number of decision points (if, case, loops, &&, ||, ?:). Each if or else-if adds 1. Safety standards (ISO 26262, DO-178C practice) typically ask for limits around 10 to 15 so each function can be tested thoroughly; high values call for guard clauses, table-driven logic or splitting the function.
Q11. The same code works at -O0 and fails at -O2 with a flag tested in a loop. Why?Advanced+
The flag is probably not volatile. At -O2 the compiler sees that the loop does not change the flag and hoists the read out of the loop, turning while (!done) into an infinite loop or removing the test. Declare the variable volatile (Module 14), or use proper atomics for data shared with an interrupt or another core.
Q12. Rewrite a branch to avoid it: if (x < 0) x = -x;. Is it worth it?Advanced+
Possible branch-free forms exist, such as the bit trick int m = x >> 31; x = (x ^ m) - m; (arithmetic shift of a negative int is implementation-defined, so check the compiler), but modern compilers already produce an IT block or conditional instruction for the simple version, and abs() maps to a single instruction on ARM. Write the clear version; optimise only after measuring.
Study Plan: Using Your Score on These If Else Interview Questions
Score
Meaning
Next step
90–100%
Excellent
Continue to Lesson 6.2: the switch statement.
70–89%
Good
Redo Examples 2, 5 and 8 of this lesson.
Below 70%
Revise
Re-read theory sections 2–6 and 10, then retake the quiz.
Habit to build: For every ladder you write, list the boundary values and run them. Most if bugs are off-by-one in a comparison.
Lesson 6.1 complete!
Next up: the switch statement, case labels, fall-through and jump tables.
2 Comments