Back to: C Tutorials For Beginners and Professionals

**Tail Recursion in C Language with Examples**

In this article, I will discuss **Tail Recursion in C Language** with Examples. Please read our previous article discussing **Types of Recursive Functions in C** Language with Examples. At the end of this article, you will understand the following pointers:

**What is Tail Recursion in C Language?****Multiple Real-Time Examples to Understand Tail Recursion in C Language****Advantages of Tail Recursion in C Language****Disadvantages of Tail Recursion in C Language****When Would We Use Tail Recursion in C Language?**

**What is Tail Recursion?**

Tail recursion in C language, or in any programming language, refers to a specific scenario in programming where a function makes a recursive call to itself as its final operation. This means the last thing the function does before it returns a value is to call itself.

In more technical terms, a function is tail-recursive if the calling function does not modify the final result of the recursive call. This differs from non-tail recursion, where the function might perform additional operations after the recursive call returns.

Here’s why tail recursion is important:

**Optimization:**Many compilers recognize tail recursion and optimize it. The optimization typically involves reusing the current function’s stack frame for the recursive call rather than creating a new one. This reduces the overhead of additional stack frames and can prevent stack overflow errors in cases of deep recursion.**Efficiency:**Tail recursion can be more efficient than non-tail recursion because it avoids the accumulation of stack frames. This is particularly important in languages like C, which do not inherently support advanced memory management features found in higher-level languages.

**Example to Understand Tail Recursion in C Language:**

We have already seen the example of tail recursion in our previous articles. The following is an example of Tail Recursion.

**What does tail recursion mean in C language?**

If a function calls itself and that recursive call is the last statement in a function, it is called tail recursion. After that call, there is nothing. It is not performing anything, so it is called tail recursion. For a better understanding, please have a look at the below image.

As you can see in the above image, the fun function takes some parameter n, and if n>0, there are some statements inside the if block. Furthermore, if you notice the last statement, it calls itself by a reduced value of n. So, it has to perform the operations first and then call itself.

So, you need to remember that if the last statement is a recursive function call, it is called tail recursion. This also means that all the operations will be performed at calling time only, and the function will not perform any operation at a returning time. Everything will be performed at calling time only, called tail recursion.

**Example: Tail Recursion in C Language**

The following is an example of Tail Recursion. As you can see, there is nothing; there is no operation we are performing after the recursive call, and that recursive function call is the last statement.

#include <stdio.h> void fun(int n) { if (n > 0) { printf("%d", n); fun(n-1); } } int main () { fun(3); return 0; }

**Output: 321**

**The following example is not Tail Recursion.**

As you can see in the example below, something is written (+n) along with the function call, i.e., some operation will be performed at the returning time. So, in this function, something remains that has to be performed at a returning time and hence cannot be a tail recursion. Tail recursion means that at returning time, it doesn’t have to perform anything at all.

#include <stdio.h> void fun(int n) { if (n > 0) { printf("%d", n); fun(n-1) + n; } } int main () { fun(3); return 0; }

**Tail Recursion vs Loops in C:**

Now, we will compare tail recursion with loops. The first and foremost thing that we need to remember is that every recursive function can be written using a loop, and vice versa, which is also true, i.e., every loop can be written using a recursive function.

The following is an example of Tail Recursive that we have just discussed. In our previous article, we traced this function and know that the output will be 321 when we pass the value 3 to the function.

Now, we want to write the above recursive function using a loop. The following image shows how to convert the recursive function to a loop. Here, instead of the conditional if statement, we use a while loop, and instead of the recursive function call with a reduced value of n, we directly reduce the n value by 1.

**Example: Using Loop**

The following example uses a loop and gets the same output as the recursive function. If you call the fun function bypassing the value 3, you will also get the same output 321 as in the Recursive function example.

#include <stdio.h> void fun(int n) { while (n > 0) { printf("%d", n); n--; } } int main () { fun(3); return 0; }

**Output: 321**

The output is the same, and the structure also looks similar between the recursive function and loop. So, the point I have to tell you here is that tail recursion can be easily converted into a loop.

**Which one should you choose between Tail Recursive and Loop in C Language?**

Let us decide which one is efficient. For this, we will compare the two examples we have already discussed in this article. Please have a look at the below image.

**Time Complexity:**

In terms of time Complexity, if you analyze, both the functions print the same three values. That means the amount of time spent is the same, whatever the value of ‘n’ is given. So, the time taken by both of them is the order of n, i.e., O(n).

**Space Complexity:**

The recursive function internally utilizes a stack. The value of 3 will create a total of 4 activation records in a stack. We have already done the analysis in our previous article. So, for a value n, the space taken by the Recursive mechanism is the order of n, i.e., O(n)

But in the loop, only one activation record will be created as it is not calling itself again. So, the space complexity of the loop is the order of 1, i.e., O(1), and it will create only one constant activation record.

So, the conclusion is that if you have to write a tail recursion, then it is better to convert it into a more efficient loop in terms of space. But this will not be true for every type of recursion or loop. So, in the case of Tail Recursion, the loop is efficient.

**Note:** One more point you must remember is that some compilers (under code optimization inside the compiler) will check if you have written any function, which is tail recursion, and then they will try to convert it into a loop. It means they will try to reduce space consumption and utilize only the order of 1, i.e., O(1).

**Tail Recursion Examples in C Language:**

Tail recursion is a specific kind of recursion where the recursive call is the last operation in the function. This has a special significance in programming because it can be optimized by the compiler to prevent additional stack space usage, effectively turning the recursion into a loop. Here are a couple of examples of tail recursion in C language:

**Factorial Calculation**

#include <stdio.h> // Tail recursive function to find factorial int factorial(int n, int accumulator) { if (n == 0) return accumulator; return factorial(n - 1, n * accumulator); } int main() { int number = 5; printf("Factorial of %d is %d\n", number, factorial(number, 1)); return 0; }

**Explanation:**

- The factorial function is called with two arguments: the number n and an accumulator that holds the intermediate results.
- If n is 0, the function returns the accumulator’s value, which is the factorial.
- If n is not 0, the function calls itself with n-1 and n * accumulator.
- This is tail recursive because the last action in the function is the recursive call.

**Fibonacci Series**

#include <stdio.h> // Tail recursive function to find nth Fibonacci number int fibonacci(int n, int a, int b) { if (n == 0) return a; if (n == 1) return b; return fibonacci(n - 1, b, a + b); } int main() { int number = 5; printf("Fibonacci number at position %d is %d\n", number, fibonacci(number, 0, 1)); return 0; }

**Explanation:**

- The fibonacci function takes three arguments: the position n, and two additional parameters a and b that store the previous two numbers in the Fibonacci series.
- If n is 0, it returns a; if n is 1, it returns b.
- Otherwise, it calls itself with n-1, b, and a + b.
- The function is tail recursive because the recursive call is the last operation.

**Advantages and Disadvantages of Tail Recursion in C Language:**

Tail recursion in C language, as in other programming languages, offers both advantages and disadvantages. Here’s a breakdown:

**Advantages of Tail Recursion in C Language**

**Optimized Performance and Efficiency:**Tail recursion can be easily optimized by compilers. Since the last action of a function is the recursive call, the current function’s stack frame can be replaced with the new one. This optimization, known as tail call optimization (TCO), reduces the overhead of additional stack frames.**Prevents Stack Overflow:**In languages that support tail call optimization, tail recursive functions can execute a large number of recursive calls without consuming more stack space, thus avoiding stack overflow issues.**Readability and Elegance:**Tail recursive solutions can be more readable and concise, particularly for problems that are naturally recursive, such as traversing a data structure.**Predictable Performance:**The memory and performance characteristics of tail recursive functions are more predictable than those of non-tail recursive functions, especially when the compiler optimizes them.

**Disadvantages of Tail Recursion in C Language:**

**Limited Compiler Support for TCO:**Not all C compilers implement tail call optimization. Without TCO, tail recursive functions can still cause a stack overflow if the recursion depth is too large.**Reduced Flexibility:**Since tail recursion requires the recursive call to be the last action, it can limit how you structure the logic of your function. This can make some algorithms awkward or less intuitive to implement.**Debugging Challenges:**Debugging tail recursive functions can be more challenging, especially with TCO, as the call stack may not retain all the information about previous recursive calls.**Potentially Less Intuitive:**For some programmers and in certain scenarios, tail recursive solutions can be less intuitive than iterative or non-tail recursive approaches.**Language-Specific Behavior:**The behavior and efficiency of tail recursion can vary depending on the compiler and the specific implementation of the C standard, leading to portability issues.**Complex Recursive Algorithms:**If your recursive algorithm involves complex branching, multiple recursive calls in a single function (like in tree traversals), or needs access to previous states, tail recursion is not suitable.**Readability and Maintenance:**In some cases, tail recursion might make your code less readable, especially for those not familiar with this concept. In such scenarios, an iterative approach might be more maintainable.

**When to use Tail Recursion in C Language?**

Tail recursion in C, or any programming language, is a special case of recursion where the recursive call is the last operation in the function. It has specific advantages and scenarios where it’s particularly beneficial to use. Here are some key points when considering tail recursion in C:

**Optimization of Stack Space:**Tail recursion can be optimized by the compiler to avoid adding a new frame to the call stack for every recursive call. This is because the current function frame is no longer needed after the recursive call. This optimization, known as tail call optimization (TCO), can prevent stack overflow errors in cases of deep recursion.**Performance Improvement:**When the compiler optimizes tail recursive functions, it can lead to performance improvements. The optimized tail recursion can approach the efficiency of iteration.**Use in Functional Style Code:**If you’re writing code in a functional style in C, tail recursion is a natural pattern. It allows you to express certain algorithms elegantly and succinctly.**Situations with Simple Accumulation or Convergence:**Tail recursion is especially useful in scenarios where a function needs to accumulate a result or converge to a solution, and the intermediate results do not need to be stored.**Conversion to Iterative Loops:**In situations where an iterative solution is more efficient or readable, but the algorithm naturally lends itself to recursion, tail recursion can be a stepping stone. Since tail recursive functions are easier to convert into loops, they offer a middle ground.**When Recursion Clarity is Preferred Over Iterative Complexity:**Sometimes, an algorithm might be more intuitively understood in its recursive form. If you can express it as tail recursion, you get the clarity of recursion without the usual performance penalty.

In the next article, I will discuss **Head Recursion in C Language** with Examples. In this article, I explain **Tail Recursion in C Language** with Examples. I hope you enjoy this article on tail recursion in C language with examples.

**About the Author: Pranaya Rout**

Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.