Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.
C Functions Interview Questions
Free C tutorial: output-prediction exercises, a quiz and C functions interview questions for embedded C programming.
C functions interview questions almost always test the same handful of ideas: scope, return values, storage duration and recursion. In this last lecture of the current block of our free C programming course you will predict the output of five short programs, check your answers against real compiler results, and then practice with a quiz, coding challenges and interview questions with answers. Everything here is aimed at C programming for embedded systems interviews, where a small misunderstanding about a static variable or a missing return can cost a whole firmware release.
What You Will Learn
- How to predict compiler errors and program output for scope, label, void and return mistakes
- The difference between block scope, function scope, file scope and static storage
- How compilers report an illegal goto, a void value and a missing return
- How to answer common C functions interview questions with confidence
- How to test yourself with a ten-question quiz and four coding challenges
Prerequisites
- The previous lectures on prime factors, series and recursive HCF and LCM
- Function declarations, prototypes and the return statement
- A compiler: all outputs below come from GCC with
-std=c99 -Wall -Wextra -O0
How to Use This Lecture
For each exercise, read the code, decide what the compiler or the program will do, and only then open the answer. Predicting first is the whole point: reading the answer immediately feels productive but trains nothing. Each answer was produced by compiling and running the code, not written from memory.
| Kind of variable | Lifetime | Lives in | Initialized |
|---|---|---|---|
| Automatic local | One call of the block | Stack or register | Every time, if you write an initializer |
| Static local | Whole program | Data or BSS section | Once, before main runs |
| File-scope variable | Whole program | Data or BSS section | Once, before main runs |
Output Prediction Exercises with Answers
Exercise 1: A goto across functions
static void ep_target(void)
{
done:
printf("inside target\n");
}
int main(void)
{
printf("start\n");
goto done;
ep_target();
return 0;
}
Question: what does this program print?
Show the answer
It does not compile. GCC reports error: label 'done' used but not defined for the goto in main and a warning label 'done' defined but not used in ep_target. Labels have function scope, so a goto can only jump to a label inside the same function. To leave a function, use return. Inside one function, goto cleanup; is a common and accepted pattern in embedded drivers for releasing resources on an error path.
Exercise 2: A variable hidden by an inner block
int main(void)
{
int level = 4;
if (level == 4) {
int level = 40;
level++;
printf("inner level=%d\n", level);
}
printf("outer level=%d\n", level);
return 0;
}
Question: what are the two lines of output?
Show the answer
The output is inner level=41 followed by outer level=4. The inner declaration creates a different variable that shadows the outer one for the length of the block. Incrementing it never touches the outer variable. Compile with -Wshadow to be warned about this, because accidental shadowing is a common source of bugs.
Exercise 3: Using the result of a void function
static void ep_store(int a, int b)
{
int total = a + b;
(void)total;
}
int main(void)
{
int r;
r = ep_store(6, 7);
printf("%d\n", r);
return 0;
}
Question: what is printed?
Show the answer
Nothing, because it does not compile: error: void value not ignored as it ought to be. A function declared void produces no value, so it cannot appear on the right side of an assignment. Either change the return type to int and return something, or call the function as a plain statement.
Exercise 4: A function that forgets to return
static int ep_triple(int v)
{
v = v * 3;
}
int main(void)
{
int r = ep_triple(5);
printf("r=%d\n", r);
return 0;
}
Question: what is printed, and is the program correct?
Show the answer
GCC warns no return statement in function returning non-void. On the test machine with -O0 the program printed r=15, but only by accident: the multiplication result happened to be left in the register that is used for return values. The language says that using the value of a function that reached its closing brace without returning is undefined behaviour, so another compiler, another optimization level or another processor can print any number. Always treat this warning as an error.
Exercise 5: Static and automatic variables together
static int ep_ticket(void)
{
static int next = 100;
int local = 100;
local++;
next++;
return next * 1000 + local;
}
int main(void)
{
printf("%d\n", ep_ticket());
printf("%d\n", ep_ticket());
printf("%d\n", ep_ticket());
return 0;
}
Question: what are the three lines of output?
Show the answer
The output is 101101, 102101 and 103101. The static variable next is initialized once and keeps its value between calls, so it grows by one each time. The automatic variable local is created fresh on every call, so it is always 101. This is exactly the behaviour that broke the static divisor in the recursive prime factor lecture.
Mistakes From This Chapter at a Glance
| Mistake | Symptom | Fix |
|---|---|---|
| Static state in a recursive function | Right answer once, wrong afterwards | Pass state as parameters |
| Uninitialized accumulator | Result changes from run to run | Initialize before the loop |
| Factorial in a 64-bit integer past 20! | Silent nonsense results | Use a term recurrence |
| Missing return in a recursive branch | Undefined result | Return every recursive call |
| LCM computed as a times b first | Overflow | Divide by the HCF first |
| Deep recursion on a small stack | Crash or silent corruption | Prefer a loop or bound the depth |
Ten-Question Quiz
Choose an answer before opening it.
-
In the factor loop, why is the bound written as
divisor <= value / divisor?- A. It is faster than any other form
- B. It avoids overflow of divisor times divisor
- C. It makes the output sorted
- D. It prevents division by zero
Answer
B. The division form cannot overflow, while the multiplication form can wrap for values near 32 bits.
-
What was the maximum recursion depth when the recursive factorizer processed the prime 4294967291?
- A. 5
- B. 256
- C. 65535
- D. 4294967291
Answer
C. It tries every divisor up to 65535, one frame each.
-
A recursive function keeps its divisor in a static local. What goes wrong?
- A. It cannot compile
- B. It runs slower
- C. The value left over from the first call corrupts the second call
- D. Nothing, static is the correct choice
Answer
C. Static storage keeps its value between top-level calls.
-
What happens when 21! is computed in a 64-bit unsigned integer?
- A. The program stops with an error
- B. The value wraps around silently
- C. The compiler switches to floating point
- D. The result is exact
Answer
B. Unsigned overflow wraps modulo 2 to the 64th power with no diagnostic.
-
Which factor turns one sine series term into the next?
- A. x / (2k)
- B. -x × x / ((2k)(2k + 1))
- C. x × x
- D. 1 / (2k + 1)!
Answer
B. The exponent grows by 2, two more factorial factors appear and the sign flips.
-
What are the HCF and LCM of 56 and 98?
- A. 7 and 784
- B. 14 and 392
- C. 2 and 5488
- D. 28 and 196
Answer
B. 14 times 392 equals 5488, which is 56 times 98.
-
Why does the LCM function divide by the HCF before multiplying?
- A. To make the code shorter
- B. To keep the intermediate value from overflowing
- C. Because multiplication is not allowed on unsigned types
- D. To produce the HCF as well
Answer
B. The full product of the two inputs can overflow even when the LCM fits.
-
What does GCC report when a goto names a label in a different function?
- A. A warning only
- B. A linker error
- C. An error that the label is used but not defined
- D. Nothing
Answer
C. Labels have function scope, so the label does not exist in the calling function.
-
What does this print:
int n = 2; { int n = 7; n += 3; } printf("%d", n);- A. 2
- B. 7
- C. 10
- D. 12
Answer
A. The inner n is a separate variable, so the outer n stays 2.
-
A function contains
static int c = 0; c++; return c;. What do three successive calls return?- A. 1, 1, 1
- B. 1, 2, 3
- C. 0, 1, 2
- D. 3, 3, 3
Answer
B. The static is initialized once, then incremented on every call.
Coding Challenges
- Write a function that returns how many distinct prime factors a 32-bit number has. For 360 the answer is 3.
- Write the cosine series (1 – x²/2! + x⁴/4! – …) using a term recurrence. Work out the ratio between consecutive terms yourself and test against
cos(). - Write a function that returns the HCF of three numbers by calling the two-number version twice.
- Rewrite the recursive
ep_hcfas a loop. Count the iterations for the pairs in the previous lecture and compare with the recursive call counts.
Self-check
For each challenge, write at least three test cases before you write the function: a normal case, an edge case such as 0 or 1, and a large case that could overflow.
C Functions Interview Questions and Answers
1. When would you choose recursion over a loop in embedded code?
Only when the problem is naturally recursive, such as walking a tree, and the maximum depth is small and provable. Otherwise a loop uses constant stack and is easier to analyze. Many safety standards restrict recursion for this reason.
2. What happens if a non-void function reaches its closing brace without returning?
If the caller uses the returned value, the behaviour is undefined. Compilers warn about it, and results can differ between optimization levels.
3. Can you use goto to jump from one function to another?
No. Labels have function scope. A goto within one function for cleanup is acceptable, and non-local jumps are done with setjmp and longjmp, which must be used with great care.
4. What is variable shadowing and how do you detect it?
An inner declaration with the same name hides an outer one inside its block. GCC and Clang can warn about it with -Wshadow.
5. How do static locals differ from automatic locals?
Automatic locals live for one call, usually on the stack, and are re-initialized each time. Static locals live for the whole program, are initialized once and are stored in the data or BSS section. Static locals make functions non-reentrant.
6. What does reentrant mean, and why does it matter in firmware?
A reentrant function can be interrupted and called again, by an interrupt or another task, without corrupting its own state. It must not rely on static or global data that is modified without protection.
7. How do you detect stack overflow on a microcontroller?
Fill the stack with a known pattern at start-up and check how much is overwritten, place a guard region protected by the memory protection unit, use RTOS stack watermarks, and use compiler options such as -fstack-usage and -Wstack-usage.
8. Does the compiler guarantee tail-call optimization?
No. GCC often does it at higher optimization levels, but the C standard does not require it. Code must stay correct without it.
9. Why does Euclid’s remainder method finish so quickly?
The remainder is always smaller than the divisor, and in two steps the numbers shrink by at least half. So the number of steps grows with the number of digits, not with the size of the values.
10. How do you compute the LCM of two 32-bit numbers safely?
Handle zero first, divide one number by the HCF, then multiply by the other in a 64-bit type.
11. What is the difference between int f() and int f(void)?
In C up to C17, empty parentheses mean the parameters are unspecified, so the compiler cannot check calls. (void) means no parameters. C23 makes the empty form mean no parameters as well. Writing (void) is the clear choice.
12. Does a function change the caller’s variable when it modifies a parameter?
No. C passes arguments by value, so the function works on a copy. To modify the caller’s variable, pass its address.
Best Practices, Performance and Security
- Build with
-Wall -Wextra -Wshadowand treat warnings as errors in your projects. - Give every function a prototype and one clear job.
- Avoid hidden state: prefer parameters over static variables, especially in recursion.
- Choose types wide enough for the largest result and prefer unsigned fixed-width types for arithmetic.
- Performance: function calls cost a few cycles for the call and return, so keep tiny hot-path helpers
staticand let the compiler inline them. - Security: bound recursion depth and loop counts for any input that comes from outside the device.
Summary and Key Takeaways
- Labels have function scope, so a goto cannot leave its function.
- An inner variable with the same name shadows the outer one.
- A void function has no value to assign.
- A non-void function must return on every path, or the result is undefined.
- Static variables keep their values between calls and make functions non-reentrant.
- Recursion is safe only with a provable depth bound.
Conclusion
You have worked through prime factorization, series sums, HCF and LCM, and now the scope and return rules that decide whether such functions behave. The common thread is that C does exactly what you wrote, not what you meant: silent wrap-around, shadowed names and leftover static state all produce confident wrong answers. The cure is the habit this chapter has practiced: predict, compile, measure and compare. Keep these questions handy when you prepare for C programming for embedded systems interviews, and continue with the next block of this free embedded C course to build on functions with pointers and arrays.
Frequently Asked Questions
Are these C functions interview questions enough for an embedded job?
They cover the function and scope topics that are asked most often. Also prepare pointers, memory layout, volatile, bit manipulation and interrupt handling.
Why do interviewers ask about static variables so often?
Static storage connects several embedded topics at once: memory sections, initialization, reentrancy and interrupt safety.
Should I memorize compiler error messages?
No. Understand the rule behind each message, such as label scope or void values, and you will recognize the error in any compiler.
Is it fine to ignore compiler warnings in small projects?
No. Several exercises in this lecture compile with only a warning and still behave unpredictably. Treat warnings as errors from the start.
Where can I practice more free C programming exercises?
Work through the challenges above and then continue with the next chapters of this free C programming course, where every lecture ends with practice questions.
Keep Learning C for Free
Return to the course index to see every chapter of the free C programming course for embedded systems, or move on to the next lecture.
Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments