C Language Function Interview Questions: From Basics to Advanced, Must-Brush Before Interviews!

C language functions are the core of code modularization and are frequently tested by interviewers. From parameter passing details to memory management, from recursive logic to pointer applications, each knowledge point may hide “pits”.

Today, I have compiled 10 function-related interview questions that cover 80% of commonly tested scenarios to help you quickly pass the interview!

01 Can function parameters have default values?

[Baidu 2024 C Language Written Test] Do C language function parameters support default values? For example, is the following syntax<span>void func(int a=10)</span> legal?

[Answer Analysis] No, it does not support. The C language standard (C89/C99/C11) does not have syntax for default values for function parameters, and the above syntax will result in a compilation error.

If similar functionality is needed, it can be achieved in the following ways:

  1. Define multiple overloaded functions (achieved through different parameter lists; C language does not have true overloading, so different function names must be manually defined, such as<span>func()</span> and <span>func_with_a(int a)</span>);
  2. Check if the parameter is a specific value inside the function (e.g., using<span>-1</span> as a marker for “not passed in”).

02 What determines the maximum depth of a recursive function?

[In-depth Understanding of Computer Systems Accompanying Interview Questions] What factors affect the maximum depth of recursive function calls? Why does excessive recursion lead to crashes?

[Answer Analysis] The maximum depth is mainly determined bystack space size:

  • Each recursive call allocates a stack frame on the stack (storing return addresses, parameters, local variables, etc.);
  • The stack space is limited (usually a few MB by default), and if the recursion count is too high, the stack space will be exhausted, triggering a “stack overflow” and causing the program to crash.

Extension: The stack space size can be configured through the compiler or operating system (e.g., GCC’s<span>-Wl,--stack</span> option), but it is not recommended to solve deep recursion issues by increasing stack space; a better solution is to use iteration instead.

03 How to define an array of function pointers?

[Linux Kernel Driver Development Interview Questions] Define an array of function pointers, where each element points to a function that “takes an int parameter and returns void”, and provide an example of how to use it.

[Answer Analysis] The format for an array of function pointers is:<span>return_type (*array_name[array_size])(parameter_type)</span>.

Example code:

#include <stdio.h>// Three target functionsvoid func1(int x) { printf("func1: %d\n", x); }void func2(int x) { printf("func2: %d\n", x); }void func3(int x) { printf("func3: %d\n", x); }int main() {    // Define and initialize the function pointer array    void (*func_arr[3])(int) = {func1, func2, func3};    // Call the functions in the array (similar to a "menu-driven" mode)    for (int i = 0; i < 3; i++) {        func_arr[i](i * 10); // Output: func1:0  func2:10  func3:20    }    return 0;}

Usage: To implement callback function tables, state machines, etc. (such as arrays of interrupt handling functions in the kernel).

04 What is the effect of const on function parameters?

[C Expert Programming Chapter 5 Key Points] In<span>void func(const int *p)</span>, what is the effect of<span>const</span>? Can the variable pointed to by this pointer be modified?

[Answer Analysis] <span>const</span> modifies the content pointed to by the pointer<span>p</span>, indicating thatthe variable pointed to by pointer p cannot be modified (to protect data from being accidentally modified).

Example:

int a = 10;const int *p = &a;*p = 20; // Compilation error: cannot modify content pointed to by consta = 20;  // Legal: directly modifying the variable itself is not restricted

Note:<span>const</span> only restricts modification through the current pointer and does not affect the modifiability of the variable itself.

05 How can a function return multiple values?

[ByteDance 2023 Backend Interview Questions] C language functions can only return one value; how can a function “return” multiple results? List at least two methods.

[Answer Analysis] Common solutions:

  1. Passing by pointer parameters: Pass the address of the variables to be returned into the function and modify them inside the function.

void calc(int a, int b, int *sum, int *product) {    *sum = a + b;    *product = a * b;}// Call: int s, p; calc(3,5,&s,&p); // s=8, p=15

2. Returning a structure: Encapsulate multiple values into a structure and return it.

typedef struct { int sum; int product; } Result;Result calc(int a, int b) {    Result res = {a + b, a * b};    return res;}// Call: Result r = calc(3,5); // r.sum=8, r.product=15

06 Must function declarations be before calls?

[The C Programming Language Chapter 4 Exercises] Can the following code compile normally? Why?

#include <stdio.h>int main() {    print_hello(); // Call the function    return 0;}void print_hello() { // Function defined after the call    printf("hello\n");}

[Answer Analysis] It can compile under C89 standard, but will result in a compilation error under C99 and above standards.

  • C89 allows “implicit declaration”: if a function is not declared before its call, the compiler assumes its return value is int and does not check the parameter list;
  • C99 and above require functions to be declared before use, otherwise it will result in an error (enforcing type safety).

Standard Practice: Add a declaration before the call:<span>void print_hello();</span>

07 Why can tail recursion optimize stack overflow?

[Google 2024 Algorithm Position Interview Questions] What is tail recursion? Why can it avoid the stack overflow problem of ordinary recursion?

[Answer Analysis] Tail recursion: The last operation of the function is a recursive call (with no subsequent calculations).

Example (tail recursive factorial):

// Tail recursive version: pass intermediate results through parametersint fact_tail(int n, int res) {    if (n == 0) return res;    return fact_tail(n-1, n * res); // The last step is a recursive call}// Call: fact_tail(5, 1) → 5*4*3*2*1*1=120

Optimization Principle: The compiler can recognize tail recursion and optimize it into a loop (reusing the current stack frame without adding new stack space), so no matter how many times recursion occurs, there will be no stack overflow. Ordinary recursion cannot be optimized because there are still calculations after the recursive call (such as<span>n * fact(n-1)</span><span>), which will accumulate stack frames.</span>

08 Performance comparison between functions and macros

[Common Interview Questions in Embedded Development] When implementing simple functions (such as adding two numbers), which performs better: functions or<span>#define</span> macros? Why?

[Answer Analysis] Macros have lower call overhead, but be cautious of side effects.

  • Macros are text replacements at the pre-compilation stage, with no stack overhead for function calls (parameter pushing, return address saving, etc.);
  • Function calls have fixed overhead but provide type checking, making logic safer.

Example comparison:

// Macro: No call overhead, but may have side effects (e.g., add(1,2+3) expands to 1+2+3)#define ADD(a,b) (a)+(b)// Function: Has call overhead but is type safeint add(int a, int b) { return a + b; }

Recommendation: Use macros for simple logic without side effects, and use functions (or<span>inline</span> functions) for complex logic or when type checking is needed.

09 What is the significance of the parameters and return value of the main function?

[C Primer Plus Chapter 11 Key Points] In<span>main(int argc, char *argv[])</span>, what do<span>argc</span> and <span>argv</span> mean? What is the purpose of the return value of the<span>main</span> function?

[Answer Analysis]

  • <span>argc</span>: The number of command line arguments (at least 1, including the program name itself);
  • <span>argv</span>: An array of strings that stores command line arguments (<span>argv[0]</span> is the program name, and <span>argv[1]</span> and beyond are user-provided arguments).

Example (running<span>./a.out 10 hello</span>):<span>argc=3</span>, <span>argv[0]="./a.out"</span>, <span>argv[1]="10"</span>, <span>argv[2]="hello"</span>.

Return value purpose: To inform the operating system of the program’s exit status (0 indicates normal exit, non-0 indicates abnormal exit, which can be captured by the shell).

10 Is nested function definition allowed in C language?

[Microsoft 2023 C Language Written Test] Is the following code legal? Can C language define another function inside a function?

#include <stdio.h>int main() {    void inner() { // Define inner function inside main        printf("inner\n");    }    inner();    return 0;}

[Answer Analysis] Not legal. The C language standard explicitly prohibits nested function definitions (unlike C++), and the above code will result in a compilation error.

Alternative Solutions:

  1. Define the inner function externally and limit its scope using<span>static</span>;
  2. Simulate using function pointers + block scope (C99 supports block-level function pointer declarations).

Remember: Understanding memory (stack/heap), pointers, and standard specifications is key to mastering functions.

If this content helped you, feel free to like and share it so more people can see it!

Leave a Comment