Interview Questions
Functions · Study material
C Functions Interview Questions and Answers
1. What is the difference between call by value and call by reference in C?
C only has call by value: the function gets copies of the arguments. Call by reference is simulated by passing pointers, so the function can change the caller's variables.
2. What is a function prototype, and why is it needed?
A declaration of the return type and parameter types. It lets the compiler check each call and convert arguments correctly. Since C99, calling an undeclared function is not allowed.
3. What does static mean for a local variable and for a function?
For a local variable: one instance with static storage duration, initialised once and kept between calls. For a function: internal linkage – it can only be called from its own source file.
4. What is recursion? What is a base case?
A function that calls itself on a smaller input. The base case is the input it answers directly, without recursing; without one, the recursion never stops.
5. What is the difference between int f() and int f(void)?
Before C23, int f() meant "unspecified parameters" and calls were not checked. In C23 both mean "no parameters".
6. Is the order of evaluation of function arguments defined?
No, it is unspecified. In printf("%d %d", next(), next()) either call may run first.
7. Can a function return an array?
No. It can return a pointer (to static or heap memory, never to a local array) or a struct that contains an array.
8. What is a function pointer used for?
Callbacks (qsort() comparators, event handlers), tables of operations, and choosing behaviour at run time.
9. What is the difference between inline and a macro?
An inline function is a real, type-checked function the compiler may expand in place; a macro is text substitution with no type checking and surprises with side effects such as MAX(i++, j).
10. What is a variadic function?
One that takes a variable number of arguments, declared with ... and read with va_start, va_arg and va_end from <stdarg.h>.
11. What does [[noreturn]] (or _Noreturn) mean?
The function never returns to its caller, like a wrapper around exit() or abort(). The compiler can optimise and warn accordingly.
12. What does [[nodiscard]] do in C23?
It asks the compiler to warn when a caller ignores the function's return value – useful for error codes and allocated memory.