0% found this document useful (0 votes)
34 views15 pages

Rrecursion in C Language

Tail recursion in C is a type of recursion where the recursive call is the last operation in the function, allowing for potential optimization by compilers. Static recursion involves using static variables to retain values across recursive calls, which can lead to unexpected behavior if the function is called multiple times. Both concepts are important for understanding memory efficiency and function behavior in recursive programming.

Uploaded by

Partha Gayen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views15 pages

Rrecursion in C Language

Tail recursion in C is a type of recursion where the recursive call is the last operation in the function, allowing for potential optimization by compilers. Static recursion involves using static variables to retain values across recursive calls, which can lead to unexpected behavior if the function is called multiple times. Both concepts are important for understanding memory efficiency and function behavior in recursive programming.

Uploaded by

Partha Gayen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

What is tail recursion in c language

ChatGPT said:

Tail recursion in C (and in general programming) is a specific type of recursion where the recursive
call is the last operation performed in the function before it returns a result. In other words, there is
no additional computation after the recursive call.

📌 Key Characteristics of Tail Recursion:

 The recursive call is the final statement in the function.

 No operation (like addition, multiplication, etc.) happens after the recursive call.

 Can be optimized by compilers into an iterative loop (known as tail call optimization) to save
memory.

✅ Example of Tail Recursive Function (Factorial):

CopyEdit

#include <stdio.h>

void tail_recursive_factorial(int n, int accumulator) {

if (n == 0) {

printf("Factorial: %d\n", accumulator);

return;

tail_recursive_factorial(n - 1, n * accumulator);

int main() {

int number = 5;

tail_recursive_factorial(number, 1);

return 0;

accumulator holds the intermediate result, so the function doesn’t need to wait to compute anything
after the recursive call.
❌ Non-Tail Recursive Factorial (for comparison):

CopyEdit

int factorial(int n) {

if (n == 0)

return 1;

return n * factorial(n - 1); // not tail recursive because multiplication happens after the recursive
call

✅ Why Use Tail Recursion?

 Uses less stack memory than regular recursion.

 Can be as efficient as loops when optimized.

However, note that standard C compilers like GCC do not guarantee tail call optimization unless
explicitly enabled and conditions are ideal.

Summary:

 Tail recursion is a recursive function where the recursive call is the last action.

 It's memory-efficient and can be optimized to a loop.

 Useful when writing recursive logic in a safer way.

What is Static Recursion in C?

In C, Static Recursion refers to a recursive function that uses static variables to retain values across
multiple function calls during recursion.

Static variables in C are declared using the static keyword. They preserve their value between
function calls, unlike local variables.

✅ Example: Static Recursion with Static Variable

CopyEdit

#include <stdio.h>
void static_recursive(int n) {

static int count = 0; // static variable retains value between calls

if (n > 0) {

count++;

static_recursive(n - 1);

printf("Count = %d\n", count); // This will print the final count multiple times

int main() {

static_recursive(3);

return 0;

🔎 Output:

ini

CopyEdit

Count = 3

Count = 3

Count = 3

Even though count is declared inside the function, it behaves like a global variable with function-
level scope because it's static.

📌 Key Points:

 Static recursion is not a special kind of recursion in theory, but a term used to describe
recursion involving static local variables.

 The static variable maintains its state through recursive calls.

 Useful when you want to track or accumulate a value across calls without passing it as a
parameter.

❗ Caution:
 Using static variables in recursion can lead to unexpected behavior if the function is called
multiple times (e.g., from main() more than once), because the variable retains its previous
value.

 Not thread-safe—static variables are shared among all calls in the same thread.

🆚 Static Recursion vs Tail Recursion:

Feature Static Recursion Tail Recursion

Uses static variable Yes Not necessarily

Memory use Moderate (depends on recursion depth) Can be optimized to use less stack space

Optimization Not compiler optimized by default Can be optimized into iteration

Purpose Preserves data across recursive calls Efficient recursive execution

MCQs on Recursion (C Language)

1. What is recursion in C?

A. A function calling another function


B. A function calling main()
C. A function calling itself
D. A function with a loop

✅ Answer: C

2. Which of the following is essential in a recursive function?

A. A return statement
B. An infinite loop
C. A base case
D. Global variables

✅ Answer: C

3. What will the following function return?

CopyEdit

int fun(int n) {
if (n == 0) return 1;

else return n * fun(n - 1);

printf("%d", fun(3));

A. 6
B. 3
C. 9
D. 0

✅ Answer: A (3 * 2 * 1 = 6)

4. Which data structure is used in recursion internally?

A. Queue
B. Stack
C. Linked List
D. Array

✅ Answer: B

5. What is the output of the following?

CopyEdit

void fun(int n) {

if (n > 0) {

printf("%d ", n);

fun(n - 1);

fun(3);

A. 3 2 1
B. 1 2 3
C. 3 3 3
D. 1 1 1

✅ Answer: A

6. What happens if a recursive function does not have a base case?


A. Program executes normally
B. Function runs once
C. Stack overflow error
D. Compilation error

✅ Answer: C

7. Recursion is most similar to which programming concept?

A. Iteration
B. Class
C. Pointer
D. Preprocessor

✅ Answer: A

8. Which of the following is an example of tail recursion?

CopyEdit

int fun(int n) {

if (n == 0) return 0;

return fun(n - 1);

A. Tail recursion
B. Head recursion
C. Infinite loop
D. None of these

✅ Answer: A

9. What is the time complexity of recursive factorial function?

A. O(1)
B. O(n)
C. O(log n)
D. O(n^2)

✅ Answer: B

10. Which of the following is not a valid use of recursion?

A. Sorting
B. Searching
C. Calculating factorial
D. Memory allocation

✅ Answer: D

💡 More MCQs (11–20)

11. What is printed by this code?

CopyEdit

void fun(int n) {

if (n > 0) {

fun(n - 1);

printf("%d ", n);

fun(3);

A. 3 2 1
B. 1 2 3
C. 1 3 2
D. 2 3 1

✅ Answer: B

12. In recursion, each function call is stored in:

A. Queue
B. Heap
C. Stack
D. Register

✅ Answer: C

13. What is the output?

CopyEdit

int fun(int x) {

if (x == 0) return 0;
else return x + fun(x - 1);

printf("%d", fun(3));

A. 3
B. 6
C. 9
D. 0

✅ Answer: B (3 + 2 + 1 = 6)

14. Recursion is most efficient when:

A. Function is called once


B. Problem can be divided into subproblems
C. Loop is used
D. Pointer is used

✅ Answer: B

15. Which is more memory-efficient in C: recursion or iteration?

A. Recursion
B. Iteration
C. Both are same
D. Depends on compiler

✅ Answer: B

16. Which problem is best solved using recursion?

A. Multiplication
B. Bubble sort
C. Fibonacci series
D. File handling

✅ Answer: C

17. What is returned by this function?

CopyEdit

int fun(int n) {

if (n == 0) return 0;
return fun(n - 1) + n;

fun(5);

A. 5
B. 10
C. 15
D. 0

✅ Answer: C (0+1+2+3+4+5 = 15)

18. Which of the following is true?

A. All recursive functions are infinite


B. Recursive function must have one return statement
C. Recursive functions can call themselves multiple times
D. Recursive functions are not allowed in C

✅ Answer: C

19. In indirect recursion:

A. A function calls itself directly


B. A function calls another function which again calls the original
C. No function is called
D. Functions are not used

✅ Answer: B

20. Which of the following problems is commonly solved using recursion?

A. Reversing an array
B. File I/O
C. Tree traversal
D. Bitwise operations

✅ Answer: C

21. What is the output of this code?

CopyEdit

void fun(int n) {

if (n <= 0) return;
fun(n - 1);

printf("%d ", n);

fun(3);

A. 3 2 1
B. 1 2 3
C. 2 1 3
D. 1 3 2

✅ Answer: B

22. Which recursion occurs when the recursive call is the last statement in the function?

A. Direct recursion
B. Indirect recursion
C. Head recursion
D. Tail recursion

✅ Answer: D

23. What is the main disadvantage of recursion?

A. It increases speed
B. Difficult to debug
C. No use of memory
D. No base case needed

✅ Answer: B

24. What is printed by this code?

CopyEdit

void fun(int n) {

if (n == 0) return;

printf("%d ", n);

fun(n - 1);

fun(3);
A. 3 2 1
B. 1 2 3
C. 2 3 1
D. 1 3 2

✅ Answer: A

25. Which one is NOT a type of recursion?

A. Direct recursion
B. Indirect recursion
C. Double recursion
D. Static recursion

✅ Answer: D

26. Which function will create infinite recursion?

CopyEdit

void fun() {

fun();

A. Yes
B. No
C. Only if condition is met
D. Only in C++

✅ Answer: A

27. What is the output?

CopyEdit

int fun(int n) {

if (n == 0) return 0;

return fun(n - 1) + n;

printf("%d", fun(4));
A. 10
B. 4
C. 0
D. 6

✅ Answer: A (0+1+2+3+4 = 10)

28. Tail recursion can be optimized by the compiler to:

A. Loop
B. Goto
C. Macro
D. Nothing

✅ Answer: A

29. Indirect recursion involves:

A. Self calls
B. Function A calling B, and B calling A
C. Static variables
D. No parameters

✅ Answer: B

30. How many times will this function print "Hi"?

CopyEdit

void fun(int n) {

if (n <= 0) return;

printf("Hi ");

fun(n - 1);

fun(3);

A. 1
B. 2
C. 3
D. Infinite

✅ Answer: C
🧠 More Conceptual MCQs

31. What does head recursion do?

A. Calls itself first, then does work


B. Does work first, then calls itself
C. Doesn't call itself
D. Uses loops

✅ Answer: A

32. Recursion is often used to solve problems that can be modeled as a:

A. Sequence
B. Graph
C. Tree
D. Array

✅ Answer: C

33. When a recursive function returns, where does it return to?

A. Top of main()
B. The last instruction before the recursive call
C. Global scope
D. System memory

✅ Answer: B

34. A recursion without a base condition leads to:

A. Compilation error
B. Runtime error
C. Stack overflow
D. Logical error

✅ Answer: C

35. Which of the following problems is NOT suitable for recursion?

A. Tree traversal
B. Calculating factorial
C. Matrix multiplication
D. Tower of Hanoi

✅ Answer: C
36. The return type of a recursive function must match:

A. void
B. int
C. float
D. Its own return type

✅ Answer: D

37. How many times is fun(1) called in this code?

CopyEdit

int fun(int n) {

if (n == 1) return 1;

return fun(n - 1) + fun(n - 1);

fun(3);

A. 1
B. 2
C. 3
D. 4

✅ Answer: D

38. Which is faster: tail recursion or normal recursion (assuming optimization)?

A. Tail recursion
B. Normal recursion
C. Both same
D. None

✅ Answer: A

39. What is the output of this?

CopyEdit

int fun(int x) {

if (x == 0) return 1;

return fun(x - 1) * x;
}

printf("%d", fun(4));

A. 4
B. 24
C. 16
D. 12

✅ Answer: B (4! = 24)

40. Which of the following best defines "base case" in recursion?

A. Starting point of recursion


B. Ending point where function stops calling itself
C. Middle of recursion
D. A value returned to main()

✅ Answer: B

You might also like