0
0
Cprogramming~5 mins

Call stack behavior

Choose your learning style9 modes available
Introduction
The call stack helps the computer remember where it is in a program when functions call other functions. It keeps track of what to do next after each function finishes.
When you want to understand how functions call each other step-by-step.
When debugging to see which function caused an error.
When learning how recursion works and how the computer manages repeated calls.
When you want to know how local variables are stored during function calls.
When you want to understand why too many function calls can cause a crash (stack overflow).
Syntax
C
/* In C, the call stack is managed automatically by the system.
Each function call creates a stack frame that stores:
- Function parameters
- Local variables
- Return address

Example functions calling each other: */

#include <stdio.h>

void functionB() {
    int localB = 20;
    printf("Inside functionB: localB = %d\n", localB);
}

void functionA() {
    int localA = 10;
    printf("Inside functionA: localA = %d\n", localA);
    functionB();
}

int main() {
    functionA();
    return 0;
}
The call stack is a hidden structure that stores information about active functions.
Each time a function is called, a new stack frame is pushed onto the stack.
Examples
Functions call each other in a chain: main -> functionA -> functionB -> functionC. The call stack grows with each call and shrinks when functions return.
C
#include <stdio.h>

void functionC() {
    printf("Inside functionC\n");
}

void functionB() {
    printf("Inside functionB\n");
    functionC();
}

void functionA() {
    printf("Inside functionA\n");
    functionB();
}

int main() {
    functionA();
    return 0;
}
This example shows recursion. Each call adds a new frame to the stack until the base case is reached.
C
#include <stdio.h>

void recursiveFunction(int count) {
    if (count == 0) {
        printf("Reached base case\n");
        return;
    }
    printf("Count: %d\n", count);
    recursiveFunction(count - 1);
}

int main() {
    recursiveFunction(3);
    return 0;
}
Sample Program
This program uses recursion to show how the call stack grows and shrinks. Each recursive call adds a new level, and when the base case is reached, the calls return one by one.
C
#include <stdio.h>

void printStackLevel(int level) {
    printf("Function call level: %d\n", level);
    if (level > 0) {
        printStackLevel(level - 1);
    }
}

int main() {
    printf("Starting program\n");
    printStackLevel(3);
    printf("Program finished\n");
    return 0;
}
OutputSuccess
Important Notes
Time complexity depends on how many function calls are made; each call adds overhead.
Space complexity depends on the depth of the call stack; deep recursion uses more stack space.
A common mistake is causing infinite recursion, which leads to stack overflow and crashes.
Use recursion when the problem naturally fits repeated function calls; use loops for simple repetition to avoid deep stack usage.
Summary
The call stack keeps track of function calls and local data during program execution.
Each function call adds a new frame to the stack; returning from a function removes that frame.
Understanding call stack behavior helps debug and write better recursive functions.