Function Call Stack in C

Last Updated : 2 Jul, 2026

The function call stack is a runtime stack data structure used to manage function calls and the order of their execution in a C program. Each time a function is called, a new stack frame is pushed onto the stack, and it is automatically removed when the function returns.

  • Stores information such as function parameters, local variables, and the return address for each active function.
  • Follows the Last-In, First-Out (LIFO) principle, where the most recently called function completes execution and returns first.
C
#include <stdio.h>

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

int main() {
    printf("Inside main()\n");
    display();
    printf("Back to main()\n");

    return 0;
}

Output
Inside main()
Inside display()
Back to main()

Explanation: When main() calls display(), a new stack frame is pushed onto the call stack. After display() finishes execution, its stack frame is removed, and control returns to main().

Here's a cleaner, shorter version that matches the style of your other articles:

Execution of Functions

When a function is called, the system creates a stack frame in the call stack to store all the information required for its execution. After the function finishes, the stack frame is removed, and control returns to the calling function.

1. Stack Frame Creation

A new stack frame is created for every function call.

  • Stores the function arguments, local variables, and the return address.
  • Keeps all information related to a single function call together.

2. Function Execution

The function executes using the data stored in its stack frame.

  • The stack pointer (SP) always points to the topmost stack frame.
  • Local variables and parameters remain available until the function completes.

3. Returning from the Function

After execution, the function returns control to the calling function.

  • The current stack frame is removed (popped) from the call stack.
  • The stored return address is used to resume execution from the point where the function was called.

Example

Let's take a code example:

C
#include <stdio.h>

// Definition of function D
void D() {
    float d = 40.5f;
    printf("In function D");
}

// Definition of function C
void C() {
    double c = 30.5;
  
    printf("In function C\n");
}

// Definition of function B
void B() {
    int b = 20;
  
  	// Calling function C
  	C();
  
  	// This will be printed after C() is executed
    printf("In function B\n");
}

// Definition of function A
void A() {
    int a = 10;
  
  	// Calling function B
  	B();
    
  	// This will be printed after B() is executed
    printf("In function A\n");
}

int main() {
  
    // Calling function A from main
    A();
  
  	// Calling function D from main, it will be called
  	// after A, B and C's execution
  	D();

    return 0;
}

Output
In function C
In function B
In function A
In function D

The below images shows how this program is stored in the call stack and how it is executed.

Call Stack for Recursion

In recursion, a function calls itself to solve smaller instances of the same problem. Each recursive call creates a new stack frame in the call stack, and the frames are removed one by one after the base case is reached.

  • Each recursive function call pushes a new stack frame containing its parameters, local variables, and return address.
  • After reaching the base case, the call stack unwinds by popping stack frames in reverse order until all function calls complete.

Note: While recursion provides an easy solution to certain problems, excessive recursion depth can cause a stack overflow due to the limited size of the call stack.

Example

C
#include <stdio.h>

// Recursive function that prints current n
// and call itself two times for n - 1 till
// n is greater than 1
void f(int n) {
  	printf("F(%d)'s Stack Frame Pushed\n", n);
  	
  	// Recursive calls
  	if (n > 1) {
  		f(n - 1);
  		f(n - 1);
    }
  
  	printf("F(%d)'s Stack Frame Removed\n", n);
}

int main() {
  
  	// Calling recursive function from main()
    f(3);
    return 0;
}


Output

F(3)'s Stack Frame Pushed
F(2)'s Stack Frame Pushed
F(1)'s Stack Frame Pushed
F(1)'s Stack Frame Removed
F(1)'s Stack Frame Pushed
F(1)'s Stack Frame Removed
F(2)'s Stack Frame Removed
F(2)'s Stack Frame Pushed
F(1)'s Stack Frame Pushed
F(1)'s Stack Frame Removed
F(1)'s Stack Frame Pushed
F(1)'s Stack Frame Removed
F(2)'s Stack Frame Removed
F(3)'s Stack Frame Removed

From the above output, we can deduce which function's stack frame is first created and removed. The below diagram helps us to visualize this in call stack memory.

Comment