Bird
Raised Fist0
Cprogramming~10 mins

Call stack behavior - Step-by-Step Execution

Choose your learning style10 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.

Practice

(1/5)
1. What does the call stack do in a C program?
easy
A. Handles input and output operations
B. Stores global variables permanently
C. Manages memory allocation for dynamic data
D. Keeps track of function calls and local variables

Solution

  1. Step 1: Understand the role of the call stack

    The call stack stores information about active function calls and their local variables during program execution.
  2. Step 2: Differentiate from other memory areas

    Global variables and dynamic memory are stored elsewhere, not on the call stack.
  3. Final Answer:

    Keeps track of function calls and local variables -> Option D
  4. Quick Check:

    Call stack = function calls + locals [OK]
Hint: Call stack tracks active functions and locals [OK]
Common Mistakes:
  • Confusing call stack with heap memory
  • Thinking global variables are on the stack
  • Assuming call stack manages I/O
2. Which of the following is the correct way to declare a function in C that will be called and added to the call stack?
easy
A. void myFunction() { /* code */ }
B. function myFunction() { /* code */ }
C. def myFunction() { /* code */ }
D. func myFunction() { /* code */ }

Solution

  1. Step 1: Identify correct C function syntax

    In C, functions are declared with a return type, name, and parentheses, e.g., void myFunction().
  2. Step 2: Recognize incorrect syntax

    Keywords like function, def, or func are not valid in C.
  3. Final Answer:

    void myFunction() { /* code */ } -> Option A
  4. Quick Check:

    C function declaration uses return type + name [OK]
Hint: C functions start with return type, no keywords like def [OK]
Common Mistakes:
  • Using Python or JavaScript keywords in C
  • Omitting return type in function declaration
  • Using incorrect braces or parentheses
3. What will be the output of this C program?
void func(int n) {
    if (n == 0) return;
    printf("%d ", n);
    func(n - 1);
}

int main() {
    func(3);
    return 0;
}
medium
A. 3 2 1
B. 3 2 1 0
C. 0 1 2 3
D. 1 2 3

Solution

  1. Step 1: Trace the recursive calls

    The function prints the current value of n, then calls itself with n-1 until n is 0.
  2. Step 2: Determine printed values

    It prints 3, then 2, then 1, then stops before printing 0.
  3. Final Answer:

    3 2 1 -> Option A
  4. Quick Check:

    Print before recursion = descending order [OK]
Hint: Print before recursive call means descending output [OK]
Common Mistakes:
  • Assuming print happens after recursion
  • Including zero in output incorrectly
  • Confusing order of prints
4. Consider this code snippet:
void foo() {
    int x = 5;
    bar();
}

void bar() {
    printf("%d", x);
}

int main() {
    foo();
    return 0;
}

What is the error here?
medium
A. Missing semicolon after bar() call
B. Function foo() is missing a return type
C. Variable x is not declared in bar()'s scope
D. main() should return void

Solution

  1. Step 1: Check variable scope

    Variable x is declared inside foo(), so it is local to foo() and not visible in bar().
  2. Step 2: Identify the error cause

    bar() tries to print x, but x is undefined in bar(), causing a compile error.
  3. Final Answer:

    Variable x is not declared in bar()'s scope -> Option C
  4. Quick Check:

    Local variables are not shared across functions [OK]
Hint: Local variables can't be accessed outside their function [OK]
Common Mistakes:
  • Thinking local variables are global
  • Ignoring variable scope rules
  • Assuming implicit variable sharing
5. Given this recursive function:
int factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

int main() {
    int result = factorial(4);
    printf("%d", result);
    return 0;
}

How many call stack frames are active at the deepest point during execution?
hard
A. 3
B. 4
C. 5
D. 1

Solution

  1. Step 1: Count recursive calls for factorial(4)

    Calls happen for n=4, 3, 2, and 1 (base case triggers at n=1).
  2. Step 2: Determine deepest call stack frames

    At n=1, the deepest call, there are 4 active frames: factorial(4), factorial(3), factorial(2), and factorial(1).
  3. Final Answer:

    4 -> Option B
  4. Quick Check:

    Number of calls = 4 frames [OK]
Hint: Count calls including base case, exclude main [OK]
Common Mistakes:
  • Counting main() as a call stack frame for this question
  • Off-by-one error in counting frames
  • Assuming base case does not add a frame
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard