Programming सीखते समय एक time ऐसा आता है जब Recursion in C सबसे confusing भी लगता है और सबसे powerful भी। C language में recursion का इस्तेमाल सिर्फ college assignments तक सीमित नहीं है; यह real-world applications, data structures, algorithms और competitive programming में बहुत important role निभाता है।
Beginners के लिए recursion थोड़ा मुश्किल इसलिए लगता है क्योंकि यह function को खुद को ही call करने देता है। लेकिन interesting point यह है कि यही approach complex problems को बहुत simple बना देती है। Recursion की सबसे बड़ी ताकत यह है कि आप बड़ी problem को छोटे-छोटे हिस्सों में divide करके easily solve कर सकते हैं।
इस article में आप Recursion in C को बिलकुल zero से लेकर advanced level तक सीखेंगे। आप जानेंगे कि recursion कैसे काम करता है, base case क्या होता है, call stack कैसे build होता है, और real C programs (जैसे factorial, fibonacci, reverse string, gcd) में इसे practically कैसे use किया जाता है।
साथ ही आप recursion के types, कब recursion use करना चाहिए, कब नहीं करना चाहिए, common mistakes, tail recursion, और interview-level questions भी सीखेंगे। यानी इस पूरी guide को पढ़ने के बाद आप recursion को किसी भी exam, coding test या interview में confidently apply कर पाएँगे।
C में रिकर्सन क्या है (What is Recursion in C in Hindi) ?
Recursion in C वह technique है जिसमें कोई function खुद को बार–बार call करता है ताकि कोई बड़ी problem छोटे version में solve हो सके। इसमे दो important terms होते हैं:
A) Base Case
वह condition जहाँ recursion रुक जाता है। अगर base case नहीं लिखा तो function infinite बार खुद को call करेगा और program crash हो जाएगा।
B) Recursive Case
जहाँ function खुद को दोबारा call करता है। यही recursion का core logic है।
Example thinking:
“जब तक काम पूरा न हो जाए, function खुद को call करता रहेगा।”
ये भी पढ़ें : Python Programming Tutorial in Hindi – शुरुआत से सीखें (Beginner to Pro)
C में रिकर्सन कैसे काम करता है (How Recursion Works in C) ?
Recursion mainly Call Stack पर depend करता है।
जब भी कोई function call होता है:
- Stack memory में उसका नया copy बनता है
- Parameter और local variables store होते हैं
- Function execute होने के बाद memory free होती है
Recursion में function खुद को call करता है, इसलिए stack में कई function copies बनती हैं। यदि base case जल्दी नहीं आता, stack में जगह खत्म हो जाती है और Stack Overflow error आता है।
Example stack flow (Factorial of 4)
fact(4)
→ fact(3)
→ fact(2)
→ fact(1)
→ returns 1
हर function अपने return value को अगले level को देता है।
Recursion in C का Basic Syntax (With Example)
C में recursive function लिखने का simple syntax:
returnType functionName(parameters) {
if (base condition) {
return value;
}
return functionName(smaller value);
}
यहाँ base condition लिखना 100% जरूरी है।
Factorial Program using Recursion in C – सबसे Simple Example
Factorial Example सबसे popular है और recursion को समझने में बहुत help करता है।
Factorial using recursion in C
#include <stdio.h>
int fact(int n) {
if (n == 1)
return 1; // base case
else
return n * fact(n - 1); // recursive case
}
int main() {
int num = 5;
printf("Factorial = %d", fact(num));
return 0;
}
Call Stack Working for fact(5)
- fact(5) → 5 * fact(4)
- fact(4) → 4 * fact(3)
- fact(3) → 3 * fact(2)
- fact(2) → 2 * fact(1)
- fact(1) → returns 1
Final result:
5 × 4 × 3 × 2 × 1 = 120
Popular Examples using Recursion in C
नीचे कुछ powerful और common recursion-based programs दिए हैं:
A) Fibonacci Series using recursion in C
int fib(int n) {
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}
B) Sum of Natural Numbers
int sum(int n) {
if (n == 0)
return 0;
return n + sum(n - 1);
}
C) Reverse a String using recursion
void reverse(char *str) {
if (*str) {
reverse(str + 1);
printf("%c", *str);
}
}
D) Power Function (x^n)
int power(int x, int n) {
if (n == 0) return 1;
return x * power(x, n - 1);
}
E) GCD using Recursion (Euclid Algorithm)
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
यह fast और efficient approach है।
ये भी पढ़ें : सी प्रोग्रामिंग में इनपुट फंक्शन क्या हैं | What is input function in C programming in Hindi
रिकर्सन के प्रकार (Types of Recursion in C)
Recursion के कई प्रकार होते हैं, और interview में भी पूछे जाते हैं, जिनकी बिबरन निचे गए है :-
1. Direct Recursion
जब कोई function खुद को सीधे अपने अंदर से call करता है, तो इसे Direct Recursion कहा जाता है। यह सबसे common और आसान recursion type है। Example: factorial(n) function जो खुद को factorial(n-1) से call करता है।
2. Indirect Recursion
Indirect Recursion में एक function खुद को सीधे call नहीं करता बल्कि किसी दूसरे function के through दुबारा call होता है। Example: A() calls B(), और B() फिर A() को call करता है। यह थोड़ा complex होता है लेकिन कुछ algorithm में useful होता है।
3. Tail Recursion
Tail Recursion वह recursion है जहां recursive call function की last statement होती है। इसमें extra काम recursive call के बाद नहीं होता, इसलिए compiler इसे optimize भी कर सकता है। Tail recursion memory efficient माना जाता है।
4. Non-Tail Recursion
Non-Tail Recursion में recursive call last statement नहीं होती। Recursive call के बाद भी कुछ operations बाकी रहते हैं। जैसे factorial calculation में n * factorial(n-1) में multiplication recursive call के बाद होता है। इसलिए यह ज्यादा memory consume करता है।
5. Binary Recursion
Binary Recursion में एक function खुद को दो बार recursively call करता है। Example: Fibonacci Series (fib(n-1) और fib(n-2))। यह powerful होता है लेकिन बहुत ज्यादा computations और memory usage करता है।
C में रिकर्सन का उपयोग कब करें (When use Recursion in C)
Recursion जब powerful बनता है:
A) Divide and Conquer Algorithms में
- Merge Sort
- Quick Sort
- Binary Search
B) Tree & Graph Data Structures
- Preorder Traversal
- Inorder Traversal
- Depth-First Search (DFS)
C) Mathematical Computations
- Factorial
- Fibonacci
- Power calculations
D) Competitive Programming
- Backtracking Problems
- N-Queens
- Sudoku Solver
इन problems को recursion के बिना solve करना बहुत difficult होता है।
C में रिकर्सन कब नहीं उपयोग करना चाहिए (Common Mistakes)
कुछ situations में recursion अच्छी choice नहीं होती जैसे :
Infinite Recursion
Base case गलत या missing होने पर program crash हो जाता है।
High Memory Consumption
Recursive calls stack memory consume करते हैं।
Performance Issues
Fibonacci recursion सबसे बड़ा example है—time complexity बहुत high।
When Iteration is Simpler
Loop लिखना आसान और fast होता है।
Tail Recursion in C Explained (Advanced but Simple)
Tail recursion वह होता है जहाँ recursive call function के last step में आता है। इससे compiler optimization कर सकता है।
Example:
int tailFact(int n, int res) {
if (n == 1) return res;
return tailFact(n - 1, n * res);
}
यह fast और memory efficient होता है।
Recursion vs Iteration — कौन बेहतर है?
| Factor | Recursion | Iteration |
| Memory | ज्यादा memory use होती है | कम memory use |
| Speed | Slow (multiple calls) | Faster |
| Code | Simple & clean | लम्बा code |
| Errors | Stack overflow risk | कम risk |
Common Errors in Recursion in C (Students’ Mistakes)
Students अक्सर ये mistakes करते हैं:
- Base case miss कर देते हैं
- गलत return value लिख देते हैं
- Function को कई बार unnecessary call कर देते हैं
- Stack overflow ignore कर देते हैं
- Wrong order of recursive calls
Learning tip:
पहले base case सोचें, फिर recursive case लिखें।
Application of Recursion in C
Recursion in C का इस्तेमाल उन situations में किया जाता है जहाँ कोई बड़ी problem को छोटे-छोटे हिस्सों में बांटकर solve किया जा सकता है। सबसे ज्यादा इसका उपयोग:-
- Divide and Conquer Algorithms में मुख्य उपयोग — जैसे Quick Sort, Merge Sort, Binary Search।
- Tree Traversal (Preorder, Inorder, Postorder) में recursion सबसे आसान तरीका है।
- Graph Traversal में Depth First Search (DFS) recursion से सरल हो जाता है।
- File System Scanning — जैसे folders के अंदर subfolders खोजने में।
- Mathematical Problems solve करने में:
- Factorial
- Fibonacci Series
- GCD/LCM
- Power Calculation (xⁿ)
- Backtracking Algorithms में उपयोग — जैसे N-Queens, Maze Solver, Sudoku Solver।
- Dynamic Programming problems को top-down approach में recursion से लिखा जाता है।
- Complex logic को छोटे reusable steps में बदलकर code को readable बनाता है।
- कुछ problems (जैसे Tower of Hanoi) recursion से naturally solve होती हैं।
Advantages of Recursion in C
- Complex problems को आसान बनाता है: बड़ी और tough problems को छोटे parts में divide करके simple तरीके से solve करता है।
- Code ज्यादा साफ और readable बनता है: Recursive code loops की तुलना में छोटा और समझने में आसान होता है।
- Tree और Graph जैसी hierarchical structures में perfect: Preorder, Inorder, Postorder traversal recursion से naturally implement होते हैं।
- Divide-and-Conquer algorithms में powerful: Merge Sort, Quick Sort, Binary Search जैसे algorithms में recursion सबसे effective होता है।
- Backtracking problems को आसान बनाता है: Maze Solver, N-Queens, Sudoku Solver recursion से आसानी से लिखे जाते हैं।
- Repeated subproblems को efficiently handle करता है: Mathematical calculations (factorial, Fibonacci, gcd) recursion से clean तरीके से लिखे जाते हैं।
- कुछ problems के लिए natural solution देता है: जैसे Tower of Hanoi या directory traversal — जहाँ logic recursive nature रखता है।
- Dynamic Programming में top-down approach enable करता है: Memoization के साथ recursion बहुत powerful हो जाता है।
- Logical thinking improve करता है: Students recursion सीखकर divide-and-conquer और problem breakdown approach को समझते हैं।
- Functions को reusable बनाता है: हर recursive call खुद में independent होता है जिससे modular design possible होती है।
Disadvantages of Recursion in C
- Memory अधिक consume होती है: हर recursive call stack में जगह लेता है, जिससे memory usage बढ़ जाती है।
- Stack Overflow का खतरा: Base case missing या deep recursion होने पर program crash हो सकता है।
- Execution speed slow हो सकती है: Loop की तुलना में recursion में function calls ज्यादा होते हैं, इसलिए performance कम होती है।
- Debugging मुश्किल होता है: Multiple recursive calls की वजह से execution track करना थोड़ा complex हो जाता है।
- Overhead ज्यादा होता है: Function call overhead (push/pop operations) recursion को slow बनाता है।
- Infinite recursion का risk: गलत logic या गलत base case infinite recursion पैदा कर देता है।
- हर problem के लिए suitable नहीं: Simple loops से solve होने वाली problems में recursion unnecessary complexity लाता है।
- Large input के लिए inefficient: Fibonacci जैसे programs में recursion बहुत slow और expensive होता है।
- Iterative solutions कभी–कभी ज्यादा stable होते हैं: खासकर जब performance critical हो।
- Compiler optimization हमेशा possible नहीं: Tail recursion optimization हर C compiler में supported नहीं होता।
Memory management in Recursion in C
Recursion में memory का सबसे important role Call Stack play करता है। हर बार जब कोई function खुद को call करता है, system stack memory में उस function का नया frame create होता है। इसी वजह से recursion powerful भी है और risky भी। नीचे memory management को simple तरीके से explained किया गया है:
Memory Management कैसे काम करता है?
- C में हर function call के साथ stack memory में एक नया stack frame बनता है।
- इस frame में function के local variables, parameters और return address store होते हैं।
- Recursive calls में कई stack frames एक के ऊपर एक build होते जाते हैं।
- जब base case hit होता है, तब stack frames reverse order में free होना शुरू होते हैं (LIFO mechanism)।
- इस process को ही unwinding the stack कहते हैं।
Stack Frame में क्या–क्या store होता है?
- Function arguments
- Local variables
- Return address (कहां वापस जाना है)
- Previous stack frame की link
Memory Overflow कैसे होता है?
जब recursion बहुत deep हो जाए या base case ना लिखा हो, तो stack memory unlimited grow करती रहती है। Stack size limited होता है, इसलिए एक point पर पहुँचकर program crash हो जाता है।
Why Recursion Uses More Memory Than Iteration?
- Recursive approach में हर function call के लिए नया stack frame बनता है।
- Iteration में loop केवल एक frame में चलता है।
- इसलिए recursion memory heavy और loop memory light माना जाता है।
Memory Optimization Techniques in Recursion
- Tail Recursion का उपयोग करें (जहाँ recursive call last statement हो)।
- Unnecessary variables और बड़े data structures recursive function में avoid करें।
- Deep recursion वाली problems को iterative तरीके से solve करें।
- Mathematical functions में memoization (DP) use करके memory और calls reduce करें।
Practice Questions (Exam + Interview Based)
नीचे कुछ important practice questions दिए हैं:
- Recursion का उपयोग करके factorial निकालें
- Fibonacci series print करें
- Sum of digits of a number
- Count vowels in a string
- LCM using recursion
- Reverse a linked list (conceptual)
- Print array elements using recursion
- Binary search using recursion
- Tower of Hanoi (interview favourite)
- Find maximum in an array (recursive approach)
ये भी पढ़ें : टॉप 10 सबसे फायदेमंद सरकारी योजनाएँ | Top 10 Government Yojana 2025
निष्कर्ष (Conclusion)
Recursion in C ऐसी technique है जो किसी भी बड़े और complex problem को छोटे-छोटे manageable parts में बदलकर solve करने की superpower देती है. शुरुआत में यह थोड़ा confusing लग सकता है, लेकिन जैसे ही आप base case, recursive case और call stack की working समझ लेते हैं, recursion आपके लिए coding का सबसे interesting हिस्सा बन जाता है।
इस article में आपने recursion का complete journey सीखा—Recursion क्या है, कैसे काम करता है, इसके types, applications, advantages–disadvantages, memory management, और real C programs में इसका practical use। अब आप confidently factorial, fibonacci, tree traversal, backtracking, mathematical problems और divide-and-conquer algorithms recursion से लिख सकते हैं।
अगर आप daily छोटे-छोटे recursion programs लिखकर खुद को train करेंगे, तो C language की सबसे confusing topic भी आपकी सबसे strong skill बन जाएगी। Coding की दुनिया में recursion आपकी thinking को next level तक ले जाता है — बस शुरुआत करने की देर है!
FAQs
Q1: क्या recursion slow होता है?
Ans. Yes, क्योंकि function बार-बार call होता है।
Q2: क्या हर recursive function को loop से replace किया जा सकता है?
Ans. Mostly yes, लेकिन कुछ problems recursion से ही simple होती हैं (tree traversal).
Q3: क्या recursion beginner-friendly है?
Ans. हाँ, यदि call stack समझ आ जाए तो recursion आसान हो जाता है।
Q4: Stack overflow कब होता है?
Ans. जब recursion बहुत deep हो जाता है या base case missing हो।