For working professionals
For fresh graduates
More
5. Array in C
13. Boolean in C
18. Operators in C
33. Comments in C
38. Constants in C
41. Data Types in C
49. Double In C
58. For Loop in C
60. Functions in C
70. Identifiers in C
81. Linked list in C
83. Macros in C
86. Nested Loop in C
97. Pseudo-Code In C
100. Recursion in C
103. Square Root in C
104. Stack in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
Recursion in C is a powerful programming technique where a function calls itself to solve smaller instances of the same problem. This method helps in breaking down complex tasks into simpler ones, making the code more elegant and easier to understand. In this article, we will explore recursion in detail, including its syntax, types, working mechanism, and real-world usage in C programming.
Explore Online Software Engineering Courses from top universities.
We will also explain when recursion occurs in C, discuss direct and indirect recursion, and compare recursion with iteration. In addition, you will learn about the advantages, disadvantages, and best practices to write efficient recursive functions in C. Whether you are a beginner or looking to strengthen your C programming skills, this guide will provide you with clear concepts and practical examples.
Recursion in C is a method where a function calls itself to solve a smaller version of the original problem. This process continues until a specific condition, known as the base case, is met. The function that performs this repeated calling is known as a recursive function.
In C programming, recursion allows a function to call itself either directly or indirectly. This creates a loop-like structure without using traditional loops such as for or while. Each recursive call solves a simpler subproblem, making the code cleaner and more efficient when used correctly.
Elevate your AI and Data Science expertise with these leading programs:
Let us look at a simple example to understand recursion in C better.
Before jumping into the code, note that:
Here’s a simple program that calculates the factorial of a number using recursion:
#include <stdio.h>
// Recursive function to calculate factorial
int factorial(int n) {
if (n == 0) {
return 1; // Base case: factorial of 0 is 1
} else {
return n * factorial(n - 1); // Recursive call
}
}
int main() {
int number = 5;
// Calling the recursive function
int result = factorial(number);
printf("Factorial of %d is %d\n", number, result);
return 0;
}
Output:
Factorial of 5 is 120
Explanation:
The syntax of recursion in C is simple but requires careful attention. A recursive function should always include two important components: a base case and a recursive call. The base case stops the recursion, while the recursive call repeats the function with modified input values.
When writing a recursive function, it is important to ensure that the input values move closer to the base case. Otherwise, the recursion could continue forever, leading to a stack overflow error.
Here is the general syntax structure for recursion in C:
return_type function_name(parameters) {
if (base_condition) {
return value; // Base case: stops the recursion
} else {
// Recursive call with modified parameters
return function_name(modified_parameters);
}
}
Must Explore: Static Function in C: Definition, Examples & Real-World Applications
Recursion in C occurs when a function needs to solve a problem by breaking it down into smaller and simpler parts. Instead of solving the entire problem at once, the function repeats itself with a reduced input until a final answer is achieved. Recursion mainly happens when the solution to a problem depends on the solution to smaller versions of the same problem.
However, without a proper termination condition, recursion can run endlessly, leading to a program crash or a stack overflow error. Therefore, every recursive approach must include a way to stop the repeated calls after reaching the smallest possible subproblem.
Let us now look at a simple example to understand exactly when recursion occurs during program execution.
Example: When Recursion Happens While Summing Numbers
Before we check the code:
#include <stdio.h>
// Recursive function to calculate sum of numbers
int sum(int n) {
if (n <= 0) {
return 0; // Stop when input becomes zero or negative
}
return n + sum(n - 1); // Recursion: working on smaller input
}
int main() {
int number = 5;
// Call the recursive function
int result = sum(number);
printf("Sum of first %d natural numbers is: %d\n", number, result);
return 0;
}
Output:
Sum of first 5 natural numbers is: 15
Detailed Code Behavior:
Also read the strlen() Function in C article!
Recursion in C works by allowing a function to call itself to solve a problem step-by-step. Each recursive call creates a new copy of the function with its own set of variables and parameters. These copies stack up in memory until a base condition is satisfied. After reaching the base case, the function calls start returning one by one, and the final result is built step-by-step during the unwinding process.
The entire process depends on two main stages:
Must Explore: Enumeration (or enum) in C article!
In C programming, recursion can be classified into different types based on how the function calls happen and how the control flows. Understanding these types helps you choose the most efficient way to solve a recursive problem.
Broadly, there are two major types of recursion in C:
Let’s now explore each one step-by-step with proper examples.
Direct recursion happens when a function calls itself directly within its body. This is the most common and straightforward type of recursion in C.
Further, direct recursion can be divided into three types based on the position of the recursive call:
Let us understand each of these with clear examples.
In head recursion, the function makes the recursive call before performing any other operations. Thus, the action is deferred until the recursive calls are completed.
Here’s a simple code example:
#include <stdio.h>
// Function using head recursion
void headRecursion(int n) {
if (n == 0)
return; // Base case
headRecursion(n - 1); // Recursive call first
printf("%d ", n); // Action after recursive call
}
int main() {
headRecursion(5);
return 0;
}
Output:
1 2 3 4 5
Explanation:
In tail recursion, the function performs its operation first, then makes the recursive call as the last action. Thus, no pending operation is left after the recursive call.
Here’s a simple code snippet:
#include <stdio.h>
// Function using tail recursion
void tailRecursion(int n) {
if (n == 0)
return; // Base case
printf("%d ", n); // Action before recursive call
tailRecursion(n - 1); // Recursive call at the end
}
int main() {
tailRecursion(5);
return 0;
}
Output:
5 4 3 2 1
Explanation:
In tree recursion, a function calls itself more than once during its execution. Thus, it creates a branching or "tree-like" structure of function calls.
Let’s see an example:
#include <stdio.h>
// Function using tree recursion
void treeRecursion(int n) {
if (n == 0)
return; // Base case
printf("%d ", n); // Print current value
treeRecursion(n - 1); // First recursive call
treeRecursion(n - 1); // Second recursive call
}
int main() {
treeRecursion(3);
return 0;
}
Output:
3 2 1 0 0 1 0 0 2 1 0 0 1 0 0
Explanation:
Must Explore: Library Function in C article!
In indirect recursion, a function does not call itself directly. Instead, it calls another function, which in turn calls the original function back.
Let’s check an easy-to-follow example:
#include <stdio.h>
// Function A calls Function B
void functionB(int n);
void functionA(int n) {
if (n <= 0)
return; // Base case
printf("%d ", n);
functionB(n - 1); // Call functionB
}
// Function B calls Function A
void functionB(int n) {
if (n <= 0)
return; // Base case
printf("%d ", n);
functionA(n / 2); // Call functionA
}
int main() {
functionA(10);
return 0;
}
Output:
10 9 4 3 1
Explanation:
In C programming, both recursion and iteration help in repeating a set of instructions. However, they work differently under the hood.
Recursion uses function calls, while iteration uses looping constructs like for, while, or do-while.
Here’s a clear tabular comparison for better understanding:
Feature | Recursion in C | Iteration in C |
Basic Concept | Function calls itself repeatedly. | Set of instructions are repeated using loops. |
Termination | Achieved using a base case condition. | Achieved using a control condition in loop. |
Memory Usage | Uses more memory due to function call stack. | Uses less memory as no function stack builds up. |
Execution Speed | Comparatively slower due to overhead of multiple function calls. | Faster as loops do not require extra function calls. |
Code Size | Code looks smaller and cleaner for complex problems. | Code might be larger but easier to understand sometimes. |
Example Structures | Recursive functions like factorial, Fibonacci series. | Loops like for, while, and do-while. |
Overhead | Higher due to multiple function calls and stack maintenance. | Lower as no function call overhead exists. |
Compiler Optimization | Harder for compilers to optimize recursive functions. | Easier for compilers to optimize loops. |
Use Case | Preferred for problems naturally recursive (tree traversal, etc.). | Preferred when simple repetitions are needed. |
Let’s now understand the difference through a simple example:
We will find the sum of first N natural numbers using both recursion and iteration.
Recursion Approach
Before checking the code:
#include <stdio.h>
// Recursive function to calculate sum of first n natural numbers
int sumRecursion(int n) {
if (n == 0)
return 0; // Base case: sum of 0 numbers is 0
else
return n + sumRecursion(n - 1); // Recursive call
}
int main() {
int n = 5;
// Call recursive function
int result = sumRecursion(n);
printf("Sum using Recursion: %d\n", result);
return 0;
}
Output:
Sum using Recursion: 15
Explanation:
Iteration Approach
Now, let’s use a simple loop to solve the same problem.
#include <stdio.h>
int main() {
int n = 5, sum = 0;
// Loop to calculate sum
for (int i = 1; i <= n; i++) {
sum += i; // Add current number to sum
}
printf("Sum using Iteration: %d\n", sum);
return 0;
}
Output:
Sum using Iteration: 15
Explanation:
In C programming, recursion offers an elegant way to solve problems, but it also brings some challenges. Let us clearly understand the benefits and drawbacks of using recursive functions.
Advantages of Recursion in C
Here are some of the advantages:
Disadvantages of Recursion in C
Here are some of the disadvantages:
Writing efficient recursive functions in C is crucial to optimize memory usage and execution speed. Without proper practices, recursion can easily lead to stack overflow, slow performance, and maintenance challenges.
Let us explore some important techniques to make your recursive functions efficient and safe.
Below is a simple program that shows how memoization can make a recursive function much more efficient.
#include <stdio.h>
#define MAX 100
// Initialize memoization array with -1
int memo[MAX];
// Recursive function to calculate Fibonacci numbers
int fibonacci(int n) {
if (n <= 1)
return n;
// Check if value already calculated
if (memo[n] != -1)
return memo[n];
// Store the calculated value in memo array
memo[n] = fibonacci(n - 1) + fibonacci(n - 2);
return memo[n];
}
int main() {
int n = 10;
// Fill memo array with -1
for (int i = 0; i < MAX; i++) {
memo[i] = -1;
}
printf("Fibonacci number at position %d is %d\n", n, fibonacci(n));
return 0;
}
Output:
Fibonacci number at position 10 is 55
Explanation:
Recursion in C is a powerful programming concept that helps solve complex problems by breaking them into smaller subproblems. When used correctly, recursive functions offer elegant and readable solutions, especially for tasks like tree traversal, factorial calculation, and more.
However, while recursion brings many benefits, it must be used with caution. Missing a base case or making inefficient recursive calls can lead to stack overflow or poor performance. Thus, always analyze whether recursion is the best fit before applying it in real-world coding scenarios.
Recursion in C is a method where a function calls itself repeatedly to solve a problem in smaller parts. It continues until a specified condition, called the base case, is satisfied and the function returns a result.
The base case in recursion acts like a stopping point for recursive calls. Without a base case, recursion can continue endlessly, leading to a stack overflow. Thus, always define a clear base case in C programs.
Recursion in C provides elegant solutions for problems like tree traversal, factorials, and Fibonacci series. It reduces the amount of code needed and helps programmers solve complex problems more naturally and cleanly.
In C, recursion can be of several types like direct recursion, indirect recursion, tail recursion, head recursion, and tree recursion. Each type has a unique structure depending on when and how the recursive call happens.
Iteration uses loops like for or while to repeat code blocks, while recursion uses self-calling functions. Recursion often makes code simpler but can use more memory compared to traditional iterative approaches.
You should use recursion when problems naturally fit into smaller, repeating subproblems. Examples include working with trees, graphs, and divide-and-conquer algorithms like merge sort or quick sort in C programs.
Recursion in C is less memory-efficient than iteration because:
Yes, recursion in C can lead to infinite loops if the base condition is missing or incorrectly placed. Thus, careful design of the recursive function and thorough testing are essential to prevent such problems.
Tail recursion happens when the recursive call is the last action in a function. It allows some compilers to optimize the recursion, saving stack space and improving the performance of the program.
If recursion depth becomes too high, the program will consume too much stack memory, causing a stack overflow error. It is important to write efficient recursive functions and monitor recursion depth to avoid such issues.
To optimize recursive functions, write a solid base case, minimize extra calculations, and use memoization if needed. Also, consider tail recursion wherever possible to reduce memory overhead and improve the efficiency of recursion in C.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.