Function Interview Questions for Embedded C: Quiz and Code Challenges
57 questions: multiple choice, write-the-declaration blanks, a feature-matching table, true/false, 6 code challenges with real output and 12 detailed interview answers. Treat it as a model for function 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 — Functions
These function interview questions in multiple-choice form are common in written screening tests. Click an answer; explanations always appear.
1. What is a function prototype?
It lets the compiler check calls before it sees the definition.
2. Which is the correct way to declare a function with no parameters?
() means unspecified parameters in C.
3. How many values can a C function return directly?
Use a struct or pointers for more.
4. On the Cortex-M4 (AAPCS) the first argument is passed in:
r0–r3 carry the first four arguments.
5. Where is the return address held after a BL instruction?
BL stores the address of the next instruction in LR.
6. Which registers must a called function preserve?
These are the callee-saved registers.
7. Which keyword gives a function internal linkage?
The function is visible only in its file.
8. Why is returning the address of a local array a bug?
The pointer dangles.
9. What does an include guard prevent?
#ifndef X_H / #define X_H / #endif.
10. Which is preferred in a header for a tiny helper function?
Each file gets its own copy; no multiple-definition errors.
11. A function is defined in a.c and called in b.c. Which is needed in b.c?
The compiler needs to know the signature.
12. What happens if a non-void function reaches its end without return and the caller uses the value?
-Wreturn-type warns about it.
13. What is the difference between a parameter and an argument?
Arguments initialise the parameters.
14. A function takes five int arguments. Where does the fifth go on the Cortex-M4?
Only four fit into r0–r3.
15. Which compiler option reports calls to functions that have no prior prototype?
Both belong in a standard embedded warning set.
Section 2: Write or Evaluate (Fill in the Blanks)
Type the declaration for the first three items (spacing is flexible) and a short value for the rest.
1. Write the prototype of a function sum that takes two ints and returns an int:
int sum(int a, int b);
2. Write the prototype of a function reset with no parameters and no return value:
void reset(void);
3. Write the first line of the include guard for a header named LED_H:
#ifndef LED_H
4. int f(int a, int b) { return a * 2 + b; }: f(3, 4) =
3*2 + 4.
5. A 64-bit result is returned in registers r0 and r on the Cortex-M4
r0:r1.
6. The register that holds the return address after BL is called the register (two letters: LR means link register)
LR = link register.
7. The number of arguments passed in registers by the AAPCS is
r0–r3.
8. The keyword to give a header function a private copy per file is static
static inline.
Section 3: Which Feature Fits?
Choose the language feature for each need, as asked in function interview questions about design.
Need
Your answer
Result
The public interface of a module
header file (.h)source file (.c)static functionstatic inline functionstruct or output pointerinclude guardconst pointer parameter(void) parameter list
The implementation of the module
header file (.h)source file (.c)static functionstatic inline functionstruct or output pointerinclude guardconst pointer parameter(void) parameter list
A helper used only inside one file
header file (.h)source file (.c)static functionstatic inline functionstruct or output pointerinclude guardconst pointer parameter(void) parameter list
A one-line helper in a header
header file (.h)source file (.c)static functionstatic inline functionstruct or output pointerinclude guardconst pointer parameter(void) parameter list
Return more than one result
header file (.h)source file (.c)static functionstatic inline functionstruct or output pointerinclude guardconst pointer parameter(void) parameter list
Avoid double inclusion of a header
header file (.h)source file (.c)static functionstatic inline functionstruct or output pointerinclude guardconst pointer parameter(void) parameter list
Pass a large read-only buffer
header file (.h)source file (.c)static functionstatic inline functionstruct or output pointerinclude guardconst pointer parameter(void) parameter list
A function that must take no arguments
header file (.h)source file (.c)static functionstatic inline functionstruct or output pointerinclude guardconst pointer parameter(void) parameter list
Section 4: True or False
Statements typical of function interview questions.
1. A function can be declared many times but defined only once in a program.
Multiple compatible declarations are allowed; multiple definitions are not.
2. Arguments are matched to parameters by name.
By position.
3. int f(); in C means the function takes no arguments.
It means unspecified arguments.
4. A static function can be called from another file.
It has internal linkage.
5. A function may call itself.
Recursion (Lesson 8.3).
6. main must always return int in a hosted environment.
In freestanding embedded code main often never returns.
7. The compiler checks that a prototype in b.c matches the definition in a.c.
Not across files, unless both include the same header.
8. A leaf function may return with bx lr without saving LR.
It makes no calls that would overwrite LR.
Section 5: Code Snippet Challenges
Trace each program on paper first; the real output follows. These challenges are the code-reading half of function interview questions.
Challenge 1 — Output?: Nested calls
#include <stdio.h>staticint twice(int x) { return2 * x; }
staticint add3(int a, int b, int c) { return a + b + c; }
int main(void)
{
printf(“%d\n”, add3(twice(1), twice(2), twice(3)));
return0;
}
What is printed?
Output:
12
The arguments evaluate to 2, 4 and 6; their sum is 12.
Challenge 2 — Output?: Does f change a?
#include <stdio.h>staticvoid f(int x)
{
x = x + 10;
printf(“in f: %d\n”, x);
}
int main(void)
{
int a = 5;
f(a);
printf(“in main: %d\n”, a);
return0;
}
What is printed?
Output:
in f: 15
in main: 5
C passes arguments by value: f changes its own copy. The variable a in main stays 5 (Lesson 8.2).
Each call returns from the matching branch: 1, -1 and 0.
Challenge 5 — Find the bug: Missing return on one path
#include <stdio.h>int check(int x)
{
if (x > 5)
return1;
}
int main(void)
{
printf(“%d\n”, check(10));
return0;
}
What does GCC warn about?
Compiler result:noreturn.c: In function ‘check’:
noreturn.c:6:1: warning: control reaches end of non-void function [-Wreturn-type]
6 | }
| ^If x is not greater than 5, the function reaches the closing brace without returning a value. Using that value is undefined behavior.
Challenge 6 — Output?: Order of the arguments
#include <stdio.h>staticint f(int a, int b) { return a – b; }
int main(void)
{
int i = 2;
printf(“%d\n”, f(i, i + 1));
printf(“%d\n”, f(10, 3) * f(3, 1));
return0;
}
What is printed?
Output:
-1
14
f(2, 3) is -1. The second line is 7 × 2 = 14.
Section 6: Function Interview Questions for Embedded C (with Detailed Answers)
Function interview questions test declarations, linkage, the calling convention and safe interfaces. Click a question for a model answer.
Q1. What is the difference between declaring and defining a function?Basic+
A declaration (prototype) gives the name, return type and parameter types so calls can be checked; it generates no code. A definition includes the body and produces the machine code. A function may be declared in many places but defined exactly once.
Q2. Why do we use header files?Basic+
A header holds the declarations (prototypes, types, constants, macros) that form a module’s interface. Every file that uses the module includes the same header, so the compiler checks all calls against one declaration, and the module’s own .c file includes it too, so a mismatch between declaration and definition is caught. Include guards make repeated inclusion safe.
Q3. What is the difference between f() and f(void) in C?Intermediate+
f(void) declares a function with no parameters; calls with arguments are errors. f() declares a function with an unspecified parameter list: the compiler does not check arguments at all, and old default promotions apply. C23 makes f() equal to f(void), but most embedded code is C99 or C11, so always write (void).
Q4. How are arguments passed on ARM Cortex-M and why does it matter?Intermediate+
The AAPCS passes the first four word-sized arguments in r0–r3, a 64-bit value occupies two registers, and the rest go on the stack. The return value is in r0 (r0:r1 for 64-bit). Functions with more than four arguments need stack traffic on every call, so group parameters in a struct passed by pointer. When reading disassembly you can match the registers to the arguments.
args_iq.c
#include <stdio.h>staticint five(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
int main(void)
{
printf(“%d\n”, five(1, 2, 3, 4, 5)); /* the fifth argument travels on the stack on ARM */return0;
}
Output (verified by compiling and running)
15
Q5. What does static do on a function, and on a local variable?Intermediate+
On a function (or a global) it gives internal linkage: the name is private to the file. On a local variable it gives static storage duration: the variable lives for the whole program and keeps its value between calls, but its scope is still the function. Both uses reduce naming clashes; the second makes the function non-reentrant, so avoid it in code called from several contexts.
Q6. Why is it dangerous to return a pointer to a local variable? How do you fix it?Intermediate+
The local lives in the function’s stack frame, which is reused after return, so the pointer refers to memory that the next call will overwrite: a classic source of random corruption. Fix: let the caller supply the buffer, return a pointer to static or constant data, or return the value itself. GCC reports it with -Wreturn-local-addr.
Q7. How can you return several values from a function?Intermediate+
multi_iq.c
#include <stdio.h>typedefstruct { int q, r; } qr_t;
static qr_t divmod(int a, int b) { qr_t x = { a / b, a % b }; return x; }
staticint divmod2(int a, int b, int *q, int *r) { if (b == 0) return –1; *q = a / b; *r = a % b; return0; }
int main(void)
{
qr_t x = divmod(17, 5);
int q, r;
int rc = divmod2(17, 5, &q, &r);
printf(“%d %d | %d %d %d\n”, x.q, x.r, rc, q, r);
return0;
}
Output (verified by compiling and running)
3 2 | 0 3 2
A small struct is returned in registers or through a hidden pointer; output parameters avoid copies and allow a status return.
Q8. Compare a function-like macro with a static inline function.Intermediate+
Macro
static inline
Type checking
None
Yes
Arguments evaluated
Possibly several times
Once
Debugging
Hard
Normal (at -O0)
Scope
Global from #define
Normal C scope
Can generate types/tokens
Yes
No
Typical use
Constants, conditional compilation
Small helpers
Prefer static inline unless the preprocessor’s token handling is needed.
Q9. How do you estimate the stack use of a call chain?Advanced+
Add the frame size of each function on the deepest path: saved registers (push {r4-r7, lr}), local variables, outgoing stack arguments and alignment padding, plus the interrupt frame of eight words (32 bytes) for each possible nested exception. Tools: GCC -fstack-usage prints each function’s frame size, the linker map shows the stack region, and stack painting (fill with a pattern and check how much was overwritten) measures the real high-water mark. Recursion makes the bound infinite unless you limit the depth.
Q10. A function declared in a header but defined with a different signature compiles and crashes. Why?Advanced+
If the defining .c file does not include the header, the compiler sees only the definition when compiling that file and only the header when compiling the caller. C does not store parameter types in the object file (no name mangling), so the linker joins them without a check. The caller passes arguments according to the wrong prototype. Always include the module’s own header in its .c file.
Q11. What is a leaf function and why does it matter for performance?Advanced+
A leaf function calls no other function. It does not need to save LR, may use the caller-saved registers r0–r3 and r12 without pushing anything, and often needs no stack frame. It can be as small as a few instructions followed by bx lr. Making hot helpers leaves (or inlining them) saves cycles and stack in interrupt handlers.
Q12. How would you make a function safe to call from both main code and an ISR?Advanced+
Make it reentrant: no static or global state that is modified without protection, no calls to non-reentrant library functions (malloc, printf), and no long blocking waits. Data shared with other contexts needs volatile and atomic access or a critical section. Document in the header whether the function may be called from an interrupt.
Study Plan: Using Your Score on These Function Interview Questions
Score
Meaning
Next step
90–100%
Excellent
Continue to Lesson 8.2: pass by value versus pass by reference.
70–89%
Good
Redo Examples 1, 3 and 6 of this lesson.
Below 70%
Revise
Re-read theory sections 2–7, then retake the quiz.
Habit to build: Compile with -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes; the compiler finds most interface mistakes for you.
Lesson 8.1 complete!
Next up: pass by value, pass by pointer and what a function can change.
2 Comments