For Loop Interview Questions for Embedded C: Quiz and Code Challenges
57 questions: multiple choice, write-the-loop blanks, a loop-matching table, true/false, 6 code challenges with real output and 12 detailed interview answers. Treat it as a model for for loop 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.
Section 1: Multiple Choice Questions — for Loops
These for loop interview questions in multiple-choice form appear in most written screening tests. Click an answer; explanations always appear.
1. In what order do the parts of for (a; b; c) body execute?
Init once; then test, body, update repeatedly.
2. How many times does for (i = 0; i < 5; i++) run its body?
i = 0,1,2,3,4.
3. What is i after for (i = 0; i < 5; i++);?
The update runs, then the test fails with i = 5.
4. Which header runs 10 times?
Start at 0 and use < 10.
5. What does for (;;) do?
A missing condition is treated as true.
6. Why does for (unsigned i = 5; i >= 0; i--) never end?
The test is always true and i wraps to a huge value.
7. Which loop correctly visits a[n-1] down to a[0] with unsigned (size_t) i?
The test uses the old value and decrements.
8. Where is a variable declared in the for header visible?
C99 scope is the loop statement.
9. What does a semicolon right after the for header do?
The next statement is outside the loop.
10. Why is a floating-point loop counter discouraged?
Adding 0.1 repeatedly never gives exactly 1.0.
11. An empty delay loop disappears when compiled with -O2. How do you keep it?
volatile forces each access to memory.
12. What is the best way to get the element count of an array?
It follows the array declaration automatically.
13. Which statement about for (int i = 0, j = 9; i < j; i++, j--) is true?
i and j meet after 5 steps (0,9) (1,8) (2,7) (3,6) (4,5).
14. Which is the most robust way to wait for a hardware flag?
A missing flag must not hang the system.
15. MISRA C:2012 rule 14.2 says a for loop shall be well-formed. This means:
The loop counter must be controlled only by the header.
Section 2: Write or Evaluate (Fill in the Blanks)
Type the loop header for the first three items (spacing is flexible) and a short value for the rest.
1. Write a for header that runs 8 times with i from 0 to 7 (header only, starting with for):
for (i = 0; i < 8; i++)
2. Write the header of an infinite loop:
for (;;) or while (1)
3. Write a for header that counts 9, 7, 5, 3, 1 (using i):
for (i = 9; i > 0; i -= 2)
4. The number of passes of for (i = 3; i < 12; i += 3) is
i = 3, 6, 9.
5. The value of i after for (i = 3; i < 12; i += 3); is
The first value failing the test.
6. int s = 0; for (int i = 1; i <= 4; i++) s += i; gives s =
1+2+3+4.
7. The number of passes of for (i = 10; i > 0; i--) is
i = 10 down to 1.
8. The number of times the inner body runs in two nested loops of 4 and 5 passes is
4 x 5.
Section 3: Which Loop Fits?
Choose the loop form for each task, as asked in for loop interview questions about design.
Task
Your answer
Result
Process exactly 16 ADC channels
for with a counterwhiledo-whilefor (;;)for with a size_t counterfor counting 7 down to 0for with a timeout counterfor with a pointer
Repeat until a button is pressed
for with a counterwhiledo-whilefor (;;)for with a size_t counterfor counting 7 down to 0for with a timeout counterfor with a pointer
Read at least once, then repeat while data remains
for with a counterwhiledo-whilefor (;;)for with a size_t counterfor counting 7 down to 0for with a timeout counterfor with a pointer
Run the main program forever
for with a counterwhiledo-whilefor (;;)for with a size_t counterfor counting 7 down to 0for with a timeout counterfor with a pointer
Fill a buffer with 0xFF
for with a counterwhiledo-whilefor (;;)for with a size_t counterfor counting 7 down to 0for with a timeout counterfor with a pointer
Send 8 bits MSB first
for with a counterwhiledo-whilefor (;;)for with a size_t counterfor counting 7 down to 0for with a timeout counterfor with a pointer
Wait for a status flag with a limit
for with a counterwhiledo-whilefor (;;)for with a size_t counterfor counting 7 down to 0for with a timeout counterfor with a pointer
Walk a linked list
for with a counterwhiledo-whilefor (;;)for with a size_t counterfor counting 7 down to 0for with a timeout counterfor with a pointer
Section 4: True or False
Statements typical of for loop interview questions.
1. The update expression of a for loop runs before the first pass of the body.
It runs after each execution of the body.
2. The initialisation of a for loop runs only once.
Before the first test.
3. If the condition is false at the start, the body of a for loop never runs.
The test comes first.
4. A for loop can have more than one loop variable.
Use comma-separated declarations and updates.
5. for (int i = 0; i < n; i++): i can be used after the loop.
Its scope ends with the loop.
6. Declaring the counter volatile keeps an empty delay loop alive.
Each access must happen.
7. A software delay loop gives the same delay at -O0 and -O2.
The time per pass changes with optimisation.
8. for (i = 0; i < 10; i++) and for (i = 0; i < 10; ++i) behave differently.
The result of the update is unused, so they are identical.
Section 5: Code Snippet Challenges
Trace each program on paper first; the real output follows. These challenges are the code-reading half of for loop interview questions.
Challenge 1 — Output?: Loop variable after the loop
#include <stdio.h>int main(void)
{
int i;
for (i = 0; i < 4; i++)
;
printf(“%d\n”, i);
for (i = 10; i > 0; i -= 3)
;
printf(“%d\n”, i);
return0;
}
What are the two printed values?
Output:
4
-2
The first loop stops with i = 4. The second takes i = 10, 7, 4, 1 and then -2, which fails the test, so -2 is printed.
Challenge 2 — Output?: Triangular nested loop
#include <stdio.h>int main(void)
{
int count = 0;
for (int i = 0; i < 3; i++)
for (int j = i; j < 3; j++)
count++;
printf(“%d\n”, count);
return0;
}
How many times does count++ run?
Output:
6
The inner loop makes 3, 2 and 1 passes: 3 + 2 + 1 = 6.
Challenge 3 — Find the bug: Stray semicolon
#include <stdio.h>int main(void)
{
for (int i = 0; i < 3; i++);
{
printf(“hello\n”);
}
return0;
}
How many times is hello printed?
Output:
hello
The semicolon is the loop body, so the block after it runs once, after the loop has finished.
Challenge 4 — Output?: Weighted sum
#include <stdio.h>int main(void)
{
int a[3] = { 1, 2, 3 };
int s = 0;
for (int i = 0; i < 3; i++)
s += a[i] * (i + 1);
printf(“%d\n”, s);
return0;
}
What is printed?
Output:
14
1×1 + 2×2 + 3×3 = 14.
Challenge 5 — Output?: An unsigned char counter wraps
#include <stdio.h>int main(void)
{
unsignedchar k;
int passes = 0;
for (k = 250; k >= 250; k++)
{
passes++;
if (passes > 10) break;
}
printf(“%d\n”, passes);
return0;
}
How many passes happen before the safety break?
Output:
6
k goes 250 … 255, then wraps to 0, which fails k >= 250. The loop makes 6 passes: 250, 251, 252, 253, 254, 255.
Challenge 6 — Output?: Two variables moving together
#include <stdio.h>int main(void)
{
int i, j;
for (i = 0, j = 10; i < j; i += 2, j -= 3)
printf(“(%d,%d) “, i, j);
printf(“\n%d %d\n”, i, j);
return0;
}
What is printed?
Output:
(0,10) (2,7)
4 4
Pass 1 runs with (0,10) and pass 2 with (2,7). Then the update gives i = 4 and j = 4, the test 4 < 4 fails and the loop ends, so the final line prints 4 4.
Section 6: For Loop Interview Questions for Embedded C (with Detailed Answers)
For loop interview questions test whether you can reason about boundaries, types and timing, not only syntax. Click a question for a model answer.
Q1. Explain the three parts of a for loop and in which order they run.Basic+
Initialisation runs once. Then the condition is tested; if true the body runs, then the update, and the condition is tested again. When the condition is false the loop ends. The update is never run after the failing test.
order_iq.c
#include <stdio.h>staticint init(void) { printf(“init “); return0; }
staticint test(int i) { printf(“test(%d) “, i); return i < 2; }
staticint upd(int i) { printf(“upd “); return i + 1; }
int main(void)
{
for (int i = init(); test(i); i = upd(i))
printf(“body “);
printf(“\n”);
return0;
}
Output (verified by compiling and running)
init test(0) body upd test(1) body upd test(2)
Q2. What is an off-by-one error? Give an embedded example.Basic+
A loop that runs one pass too many or too few, usually from <= instead of <, or from starting at 1 instead of 0. Example: for (i = 0; i <= 16; i++) buf[i] = 0; on a 16-byte buffer clears the byte after the buffer, corrupting whatever variable the linker placed there, with no immediate error on a microcontroller.
Q3. How do you loop backwards over an array with an unsigned index?Intermediate+
Do not test i >= 0. Use for (i = n; i-- > 0; ), or count n down to 1 and use index k - 1, or use a signed type such as ptrdiff_t if n fits. GCC reports the always-true test with -Wtype-limits (in -Wextra).
rev_iq.c
#include <stdio.h>#include <stddef.h>int main(void)
{
int a[] = { 10, 20, 30 };
for (size_t i = sizeof a / sizeof a[0]; i– > 0; )
printf(“%d “, a[i]);
printf(“\n”);
return0;
}
Output (verified by compiling and running)
30 20 10
Q4. How do you write an infinite loop, and how do you leave it safely?Intermediate+
for (;;) or while (1). Bare-metal firmware normally never leaves its main loop; for a finite state use a condition variable such as running or a break/return on a defined event. Some static-analysis rules (MISRA 15.4) restrict the number of break statements per loop.
Q5. Why can an empty delay loop fail, and what should you use instead?Intermediate+
With optimisation on, the compiler removes a loop without side effects. Even with volatile the delay depends on compiler, flags, clock speed, flash wait states and interrupts. Use a timer: poll SysTick, use a general-purpose timer with an interrupt, or the DWT cycle counter for microsecond delays; for long waits use low-power sleep with a timer wake-up.
Q6. How can you make a loop faster on a Cortex-M4?Intermediate+
Take invariant expressions out of the loop (strlen in the condition), count down to zero so that the decrement sets the flags, use word-sized accesses instead of byte accesses (copy 4 bytes at a time), unroll small fixed loops (#pragma GCC unroll), and replace hand-written loops by memcpy, DMA or CMSIS-DSP. Measure with the DWT cycle counter; the optimiser often does the first three for you.
Q7. What is wrong with for (float f = 0; f != 1.0f; f += 0.1f)?Intermediate+
0.1 cannot be represented exactly in binary floating point, so f never equals 1.0f exactly and the loop does not end (or ends one pass late with <=). Use an integer counter: for (int k = 0; k <= 10; k++) { float f = k / 10.0f; ... }.
Q8. Reverse the bits of a byte using a for loop.Intermediate+
reverse_iq.c
#include <stdio.h>#include <stdint.h>staticuint8_t rev(uint8_t x)
{
uint8_t r = 0;
for (int i = 0; i < 8; i++)
{
r = (uint8_t)((r << 1) | (x & 1u));
x >>= 1;
}
return r;
}
int main(void)
{
printf(“0x%02X -> 0x%02X\n”, 0x2C, rev(0x2C));
return0;
}
Output (verified by compiling and running)
0x2C -> 0x34
The Cortex-M4 can do this in one instruction with __RBIT() for 32-bit words, followed by a shift for smaller sizes.
Q9. What is loop unrolling? Show an example and name the cost.Advanced+
unroll_iq.c
#include <stdio.h>#include <stdint.h>int main(void)
{
uint8_t a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
unsigned sum = 0;
for (int i = 0; i < 8; i += 4) /* four elements per pass */
{
sum += a[i];
sum += a[i + 1];
sum += a[i + 2];
sum += a[i + 3];
}
printf(“%u\n”, sum);
return0;
}
Output (verified by compiling and running)
36
Unrolling repeats the body so the compare-and-branch overhead is paid once per four elements instead of once per element. Costs: larger code (flash), more registers in use, and a leftover loop when the count is not a multiple of the unroll factor. On a Cortex-M4 with flash wait states the larger code can even run slower.
Q10. A for loop reading a hardware flag hangs the product in the field. How do you design it?Advanced+
Bound every wait: for (n = TIMEOUT; n > 0 && !(SR & FLAG); n--) ; then test n == 0 and report an error (return a status, set a fault flag, reset the peripheral). Use a time-based timeout from SysTick rather than an iteration count when the clock may change. Feed the watchdog only from code paths that have proven progress.
Q11. How does the compiler treat a loop with a known small trip count?Advanced+
At -O2/-O3 GCC may fully unroll loops with a small constant trip count (the loop disappears and each iteration is straight-line code), vectorise them where SIMD is available (Cortex-M55/M85 with Helium, not on the M4), or replace patterns by library calls (a loop that zeroes memory becomes memset). This is why the same code can look very different in the disassembly at -O0 and -O2.
Q12. Why does MISRA restrict modifying the loop counter inside the body?Advanced+
When the counter changes in the body, the number of passes is no longer visible in the header, which makes review, testing and worst-case execution time analysis hard. MISRA C:2012 rule 14.2 asks for well-formed for loops: a single loop counter that is not modified in the body, the condition testing that counter, and the update expressions as the only place it changes. If you need such logic use a while loop.
Study Plan: Using Your Score on These For Loop Interview Questions
Score
Meaning
Next step
90–100%
Excellent
Continue to Lesson 7.2: while and do-while loops.
70–89%
Good
Redo Examples 1, 4 and 6 of this lesson.
Below 70%
Revise
Re-read theory sections 1–5 and 8, then retake the quiz.
Habit to build: Before running any loop, write the first, the second-to-last and the last value of the counter, and check that those are the values you want.
Lesson 7.1 complete!
Next up: while and do-while loops, sentinel values and polling.
2 Comments