0
0
Cprogramming~10 mins

Call stack behavior - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Call stack behavior
Program starts
Call main()
Call function A
Call function B
Function B returns
Function A returns
main() returns
Program ends
This flow shows how functions are called and returned, stacking and unstacking on the call stack.
Execution Sample
C
#include <stdio.h>

void B() {
    printf("In B\n");
}

void A() {
    B();
    printf("Back in A\n");
}

int main() {
    A();
    return 0;
}
This code calls function A from main, which calls function B, then returns back step-by-step.
Execution Table
StepActionCall StackOutput
1Program starts, main() calledmain
2main() calls A()main -> A
3A() calls B()main -> A -> B
4B() executes printfmain -> A -> BIn B
5B() returnsmain -> A
6A() executes printfmain -> ABack in A
7A() returnsmain
8main() returns
9Program ends
💡 All functions returned, call stack is empty, program ends.
Variable Tracker
Call StackStartAfter Step 2After Step 3After Step 5After Step 7Final
Stack Framesmain -> Amain -> A -> Bmain -> Amain
Key Moments - 3 Insights
Why does the call stack grow when a function is called?
Each function call adds a new frame to the stack to keep track of where to return and local variables, as shown in steps 2 and 3 of the execution_table.
When does the call stack shrink?
The stack shrinks when a function finishes and returns, removing its frame, as seen in steps 5, 7, and 8.
Why is the output 'In B' printed before 'Back in A'?
Because B() runs first and prints before returning to A(), which then prints, matching the call stack order in steps 4 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the call stack?
Amain -> B
Bmain -> A -> B
Cmain -> A
DA -> B
💡 Hint
Check the 'Call Stack' column at step 3 in the execution_table.
At which step does function B() return and its frame is removed from the stack?
AStep 6
BStep 4
CStep 5
DStep 7
💡 Hint
Look for 'B() returns' in the 'Action' column of the execution_table.
If function A() did not call B(), how would the call stack at step 3 change?
AIt would be main -> A
BIt would be main only
CIt would be main -> A -> B
DIt would be empty
💡 Hint
Refer to the call stack growth logic in the key_moments and execution_table.
Concept Snapshot
Call stack stores function calls in order.
Each call adds a frame on top.
Return removes the top frame.
Stack shows current active functions.
Output order follows call and return sequence.
Full Transcript
This example shows how the call stack works in C. When main() calls A(), a frame for A is added on top of main's frame. Then A() calls B(), adding B's frame on top. B() runs and prints 'In B', then returns, removing its frame. Control goes back to A(), which prints 'Back in A' and returns, removing its frame. Finally, main() returns and the program ends. The call stack grows with each call and shrinks with each return, managing the order of execution and local data.