C Recursion, Call Stack Mechanics & 5 Modular Software Projects
Welcome to Phase 6 (Chapter 13): C Recursion, CPU Call Stack Mechanics & 5 Modular Projects Masterclass! Recursion is one of computer science's most elegant algorithmic paradigms, allowing elegant solutions to divide-and-conquer problems, tree traversals, and mathematical sequences. In this comprehensive guide, you will master the internal CPU Call Stack activation records during recursive self-invocations, base case boundary rules, preventing catastrophic stack overflows, and construct 5 complete production-grade modular C libraries and projects.
Recursion ante oka function thanani thane smaller sub-problem tho call chesukovadam. Every recursive function must strictly satisfy 2 fundamental laws:
- 1. The Base Case (Stopping Condition): Recursion infinite loop lo vellakunda terminate chese boundary check (e.g.
if (n <= 1) return 1;). Missing base case causes a fatal Segmentation Fault / Stack Overflow Crash! - 2. The Recursive Step: Problem size ni reduce chesthu smaller input tho self-call cheyyadam (e.g.
return n * factorial(n - 1);).
[ 1. PUSHING FRAMES ON STACK ] [ 2. UNWINDING & MULTIPLYING ]
โ factorial(1) -> returns 1 (Base Case) โ returns 1
โ factorial(2) -> 2 * factorial(1) โ returns 2 * 1 = 2
โ factorial(3) -> 3 * factorial(2) โ returns 3 * 2 = 6
โ factorial(4) -> 4 * factorial(3) โ returns 4 * 6 = 24
โ main() โ main() receives 24!
Modular software design across 5 practical real-world modules:
Projects 1 & 2: Modular Calculator & Student Marks Grading System
#include <stdio.h>
// --- 1. Modular Calculator Library ---
double calculate(double a, double b, char op) {
if (op == '+') return a + b;
if (op == '-') return a - b;
if (op == '*') return a * b;
if (op == '/') return (b != 0.0) ? (a / b) : 0.0;
return 0.0;
}
// --- 2. Student Marks & Grading Engine ---
char assignGrade(double avg) {
if (avg >= 90.0) return 'A';
if (avg >= 75.0) return 'B';
if (avg >= 50.0) return 'C';
return 'F';
}
int main(void) {
printf("1. Calculator: 120 / 4 = %.2f\n", calculate(120, 4, '/'));
printf("2. Student Avg (88.5%%) -> Grade: %c\n", assignGrade(88.5));
return 0;
}
Projects 3, 4 & 5: Number Utility Library, Unit Converter & Recursion Suite
#include <stdio.h>
#include <stdbool.h>
// --- 3. Number Utility Library ---
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
// Recursive Factorial
long long factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
// --- 4. Unit Converter Library ---
double cToF(double c) { return (c * 9.0 / 5.0) + 32.0; }
double kmToMiles(double km) { return km * 0.621371; }
int main(void) {
printf("3. Number Utility: Is 47 Prime? %s\n", isPrime(47) ? "YES" : "NO");
printf("4. Recursion: 5! = %lld\n", factorial(5));
printf("5. Converter: 100 km = %.2f Miles | 37ยฐC = %.1fยฐF\n", kmToMiles(100), cToF(37));
return 0;
}
Run this recursive Fibonacci calculation program in our online GCC compiler:
#include <stdio.h>
int fibonacci(int n) {
if (n <= 0) return 0;
if (n == 1) return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main(void) {
printf("Fibonacci term 7: %d\n", fibonacci(7));
return 0;
}