C Programming Tricky Questions
Tricky Questions on Functions and Recursion
What is the difference between function declaration and definition?
Declaration tells the compiler about function name, return type, and parameters (prototype). Definition provides the actual implementation (body). A function can be declared multiple times but defined only once.
What is recursion and what are its two essential components?
Recursion is when a function calls itself. The two essential components are: base case (termination condition) and recursive case (function calls itself with modified parameters). Missing base case causes infinite recursion.
Tricky Point: Every recursive algorithm can be implemented iteratively, but recursion is often more elegant for problems with recursive structure.
What is tail recursion and why is it important?
Tail recursion occurs when the recursive call is the last operation in the function. Some compilers optimize tail recursion to iterative loops, avoiding stack overflow and improving performance.
What is the call stack and how does it relate to recursion?
The call stack tracks function calls. Each recursive call pushes a new frame onto the stack. Deep recursion can cause stack overflow if too many frames accumulate.
What is the difference between pass by value and pass by reference in C?
C only has pass by value. However, you can simulate pass by reference by passing pointers. The pointer value (address) is passed by value, but through it you can modify the original variable.
What is mutual recursion?
Mutual recursion occurs when two or more functions call each other in a cycle. Function A calls function B, which calls function A. This requires forward declarations.
What is function pointer and when is it useful?
A function pointer stores the address of a function. Useful for callback functions, implementing function tables, and creating flexible architectures where behavior can be changed at runtime.
What is variable-length argument list and which functions support it?
Variable-length argument lists allow functions to accept varying numbers of arguments. Implemented using stdarg.h macros (va_list, va_start, va_arg, va_end). Example: printf().
What is the difference between iterative and recursive solutions?
Iterative solutions use loops, recursive solutions use function calls. Recursion often has clearer logic for recursive problems but uses more memory (stack). Iteration is usually more memory efficient.
What is inline function and how is it different from macro?
Inline functions are expanded at compile time (suggestion to compiler). They provide type checking and debugging benefits over macros. Unlike macros, they respect scope and namespace rules.
What is recursion depth and what limits it?
Recursion depth is the number of recursive calls before reaching base case. Limited by stack size. Deep recursion can cause stack overflow. Tail recursion optimization can mitigate this.
Can main() function be called recursively?
Yes, but it's generally bad practice. The C standard allows it, but recursive main() can cause unexpected behavior with program initialization and termination.
Warning: Recursive main() makes code hard to understand and debug. Avoid it in production code.
What is static function and what's its scope?
Static functions have file scope - they're only visible within the file where defined. This provides encapsulation and prevents naming conflicts across files.
What is memoization in recursion?
Memoization stores results of expensive function calls and returns cached result when same inputs occur again. Dramatically improves performance for recursive algorithms with overlapping subproblems (like Fibonacci).
What is the difference between actual and formal parameters?
Actual parameters (arguments) are values passed to function during call. Formal parameters are variables declared in function definition that receive the values.
What is indirect recursion?
Indirect recursion occurs when function A calls function B, which calls function C, which calls function A. This creates a cycle through intermediate functions.
What is variadic function and what are its limitations?
Variadic functions accept variable number of arguments. Limitations: no type safety (compiler can't check argument types), must have at least one named parameter, difficult to use correctly.
What is the time and space complexity of recursive Fibonacci?
Naive recursive Fibonacci has O(2ⁿ) time complexity (exponential) and O(n) space complexity (call stack depth). With memoization, improves to O(n) time and O(n) space.
What is function overloading and does C support it?
Function overloading allows multiple functions with same name but different parameters. C does NOT support function overloading - each function must have unique name. C++ supports it.
What is recursive data structure?
Data structures that contain references to instances of same type. Examples: linked lists (node points to node), trees (node points to child nodes). Naturally processed with recursion.
What is the difference between library function and user-defined function?
Library functions are part of standard library (printf(), scanf()). User-defined functions are created by programmer. Both follow same syntax rules.
What is infinite recursion and how can it be prevented?
Infinite recursion occurs when no base case or base case never reached. Causes stack overflow. Prevent by ensuring recursive case moves toward base case and base case is reachable.
What is the difference between recursion and iteration in terms of memory?
Recursion uses stack memory for each call (parameters, return address, local variables). Iteration uses constant memory (loop variables). Recursion can cause stack overflow for deep calls.
What is a pure function and what are its benefits?
Pure functions always produce same output for same input and have no side effects. Benefits: easier to test, debug, parallelize, and reason about. Recursive functions are often pure.
What is the maximum recursion depth in C?
No fixed maximum in C standard - depends on stack size and available memory. Typically limited by system constraints. Can be increased with compiler flags or iterative implementation.
Tricky Point: The actual limit varies by platform, compiler, and available memory. Stack overflow occurs when recursion depth exceeds available stack space.
Note: These tricky questions cover fundamental concepts about C functions and recursion, including parameter passing, memory usage, optimization techniques, and common pitfalls. Understanding these concepts is crucial for writing efficient recursive algorithms and well-structured functions.