C Tutorial — Learn C Programming
Key takeaway: This free C tutorial teaches you systems programming from scratch — variables, pointers, arrays, functions, structs, and memory management — with practical code examples you can compile and run.
C is a general-purpose, procedural programming language created in 1972. It is used for operating systems, embedded systems, game engines, and performance-critical applications. This tutorial is for beginners, CS students, and developers who want to understand how computers work at a low level.
Last updated: September 2026
Recursion in C
Recursion is a programming technique where a function calls itself to solve a problem. It's particularly useful for problems that can be broken down into smaller, similar subproblems.
Basic Recursion Concept
#include <stdio.h>
// Recursive function to calculate factorial
int factorial(int n) {
// Base case
if (n == 0 || n == 1) {
return 1;
}
// Recursive case
else {
return n * factorial(n - 1);
}
}
int main() {
int num = 5;
int result = factorial(num);
printf("Factorial of %d is: %d\n", num, result);
return 0;
}Fibonacci Sequence
#include <stdio.h>
int fibonacci(int n) {
// Base cases
if (n <= 1) {
return n;
}
// Recursive case
return fibonacci(n - 1) + fibonacci(n - 2);
}
void printFibonacci(int terms) {
printf("Fibonacci sequence: ");
for (int i = 0; i < terms; i++) {
printf("%d ", fibonacci(i));
}
printf("\n");
}
int main() {
int terms = 10;
printFibonacci(terms);
return 0;
}Power Calculation
#include <stdio.h>
int power(int base, int exponent) {
// Base case
if (exponent == 0) {
return 1;
}
// Recursive case
return base * power(base, exponent - 1);
}
int main() {
int base = 2, exp = 5;
int result = power(base, exp);
printf("%d^%d = %d\n", base, exp, result);
return 0;
}Sum of Natural Numbers
#include <stdio.h>
int sumNatural(int n) {
// Base case
if (n == 1) {
return 1;
}
// Recursive case
return n + sumNatural(n - 1);
}
int main() {
int num = 10;
int sum = sumNatural(num);
printf("Sum of first %d natural numbers: %d\n", num, sum);
return 0;
}Important Notes:
- Base Case: Every recursive function must have a base case to stop recursion
- Stack Overflow: Too many recursive calls can cause stack overflow
- Performance: Recursive solutions may be less efficient than iterative ones
- Memory Usage: Each recursive call uses stack memory