Quiz-Function Interview Questions for Embedded C: Quiz and Code Challenges-Free Embedded C Programming Course

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

MODULE 8 · LESSON 8.1 · PRACTICE

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?

2. Which is the correct way to declare a function with no parameters?

3. How many values can a C function return directly?

4. On the Cortex-M4 (AAPCS) the first argument is passed in:

5. Where is the return address held after a BL instruction?

6. Which registers must a called function preserve?

7. Which keyword gives a function internal linkage?

8. Why is returning the address of a local array a bug?

9. What does an include guard prevent?

10. Which is preferred in a header for a tiny helper function?

11. A function is defined in a.c and called in b.c. Which is needed in b.c?

12. What happens if a non-void function reaches its end without return and the caller uses the value?

13. What is the difference between a parameter and an argument?

14. A function takes five int arguments. Where does the fifth go on the Cortex-M4?

15. Which compiler option reports calls to functions that have no prior prototype?

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:

2. Write the prototype of a function reset with no parameters and no return value:

3. Write the first line of the include guard for a header named LED_H:

4. int f(int a, int b) { return a * 2 + b; }: f(3, 4) =

5. A 64-bit result is returned in registers r0 and r on the Cortex-M4

6. The register that holds the return address after BL is called the register (two letters: LR means link register)

7. The number of arguments passed in registers by the AAPCS is

8. The keyword to give a header function a private copy per file is static

Section 3: Which Feature Fits?

Choose the language feature for each need, as asked in function interview questions about design.

NeedYour answerResult
The public interface of a moduleheader file (.h)source file (.c)static functionstatic inline functionstruct or output pointerinclude guardconst pointer parameter(void) parameter list
The implementation of the moduleheader file (.h)source file (.c)static functionstatic inline functionstruct or output pointerinclude guardconst pointer parameter(void) parameter list
A helper used only inside one fileheader file (.h)source file (.c)static functionstatic inline functionstruct or output pointerinclude guardconst pointer parameter(void) parameter list
A one-line helper in a headerheader file (.h)source file (.c)static functionstatic inline functionstruct or output pointerinclude guardconst pointer parameter(void) parameter list
Return more than one resultheader file (.h)source file (.c)static functionstatic inline functionstruct or output pointerinclude guardconst pointer parameter(void) parameter list
Avoid double inclusion of a headerheader file (.h)source file (.c)static functionstatic inline functionstruct or output pointerinclude guardconst pointer parameter(void) parameter list
Pass a large read-only bufferheader file (.h)source file (.c)static functionstatic inline functionstruct or output pointerinclude guardconst pointer parameter(void) parameter list
A function that must take no argumentsheader 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.

2. Arguments are matched to parameters by name.

3. int f(); in C means the function takes no arguments.

4. A static function can be called from another file.

5. A function may call itself.

6. main must always return int in a hosted environment.

7. The compiler checks that a prototype in b.c matches the definition in a.c.

8. A leaf function may return with bx lr without saving 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> static int twice(int x) { return 2 * x; } static int add3(int a, int b, int c) { return a + b + c; } int main(void) { printf(“%d\n”, add3(twice(1), twice(2), twice(3))); return 0; }

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> static void 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); return 0; }

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).
Challenge 3 — Output?: A static local variable
#include <stdio.h> static int counter(void) { static int n = 0; n++; return n; } int main(void) { counter(); counter(); printf(“%d\n”, counter()); return 0; }

What is printed?

Output:
3
The static variable keeps its value between calls: after three calls n is 3.
Challenge 4 — Output?: Multiple returns
#include <stdio.h> static int pick(int x) { if (x > 0) return 1; else if (x < 0) return –1; return 0; } int main(void) { printf(“%d %d %d\n”, pick(9), pick(-9), pick(0)); return 0; }

What is printed?

Output:
1 -1 0
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) return 1; } int main(void) { printf(“%d\n”, check(10)); return 0; }

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> static int 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)); return 0; }

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> static int 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 */ return 0; }
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> typedef struct { int q, r; } qr_t; static qr_t divmod(int a, int b) { qr_t x = { a / b, a % b }; return x; } static int divmod2(int a, int b, int *q, int *r) { if (b == 0) return –1; *q = a / b; *r = a % b; return 0; } 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); return 0; }
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+
Macrostatic inline
Type checkingNoneYes
Arguments evaluatedPossibly several timesOnce
DebuggingHardNormal (at -O0)
ScopeGlobal from #defineNormal C scope
Can generate types/tokensYesNo
Typical useConstants, conditional compilationSmall 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

ScoreMeaningNext step
90–100%ExcellentContinue to Lesson 8.2: pass by value versus pass by reference.
70–89%GoodRedo Examples 1, 3 and 6 of this lesson.
Below 70%ReviseRe-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.

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *