C Recursion

C Recursion Tutorial
Recursion in C language is a technique where a function calls itself to solve a problem by breaking it into smaller sub-problems.
What is Recursion?
A function that calls itself directly or indirectly is called a recursive function.
Every recursive function must have:
Base condition (stopping case)
Recursive call (self call)
Basic Recursion Syntax
Simple Recursion Example
Print Numbers 1 to 5
Output: 1 2 3 4 5
Factorial Using Recursion (Interview Favorite)
5! = 120
Fibonacci Series Using Recursion
- Inefficient for large values
Recursion vs Iteration
| Feature | Recursion | Iteration |
|---|---|---|
| Code | Simple | Complex |
| Memory | More (stack) | Less |
| Speed | Slower | Faster |
| Debugging | Hard | Easy |
- Use recursion when logic is naturally recursive
Stack Working in Recursion (Very Important)
Each recursive call:
Gets its own stack frame
Stores local variables & return address
Deep recursion → Stack overflow
Tail Recursion (Advanced)
Indirect Recursion
Common Mistakes
- Missing base condition
- Infinite recursion
- Large input causing stack overflow
- Wrong return logic
Interview Questions (Must Prepare)
What is recursion?
Base condition importance?
Difference between recursion & iteration
What is stack overflow?
Tail recursion
Indirect recursion
Real-Life Use Cases
Tree & graph traversal
Divide & conquer algorithms
Backtracking
Compiler design
OS process handling
Summary
- Recursion simplifies complex problems
- Requires proper base condition
- Uses stack memory
- Very important for DSA & interviews
