0 · Recall Warm-up
Answer from memory first. Answers are in the Answer Vault at the bottom.
- A static local is initialized how many times? from Lesson 1.3.2
- What does
while (n--)leave in n when it ends? from Lesson 1.2.2 - How many bytes does a
doubletake on our machine? from Lesson 1.1.1
1 · The Hook
🎣
In a canteen, clean plates are stacked: you always put a plate on top and always take the top one. Function calls behave exactly like that pile. When main calls a, a calls b, and b calls c, the last one started (c) must finish first, then b resumes, then a.
2 · Predict First
🤔
What does this print?
void c(void) { printf("c "); }
void b(void) { printf("b1 "); c(); printf("b2 "); }
void a(void) { printf("a1 "); b(); printf("a2 "); }
int main(void)
{
a();
printf("\n");
return 0;
}
Make a guess in your head. The answer opens Section 3.
3 · Core Concept
💡
Predict First — the answer
Output
a1 b1 c b2 a2
Each function pauses at its call, lets the called function run completely, then continues where it paused. The last function called is the first to finish.
The stack
A stack is a pile where items are added (pushed) and removed (popped) only at the top: last in, first out. The running program keeps one such pile in memory, the call stack.
Stack frames
Each call pushes a stack frame (also called an activation record) holding:
- the parameters (the copied argument values),
- the local (automatic) variables,
- the return address: where to continue in the caller afterwards,
- some saved bookkeeping, such as the caller’s frame position.
When the function returns, its frame is popped and all its locals disappear. That is why automatic variables die with the call. Statics and globals are not in frames; they live in their own area.
Tracing frames
int sq(int n)
{
int r = n * n;
return r;
}
int sumsq(int x, int y)
{
int s = sq(x) + sq(y);
return s;
}
int main(void)
{
int v = sumsq(3, 4);
printf("%d\n", v);
return 0;
}
At the moment the first sq(3) is running, the stack holds three frames:
top -> | sq : n = 3, r = 9, return to sumsq |
| sumsq : x = 3, y = 4, s = ?, return to main |
bottom | main : v = ? |
sq returns 9 and is popped; sumsq calls sq(4) (a new frame with n = 4), gets 16, computes s = 25, returns, and main prints:
Output
25
Depth and number of calls
The number of frames on the stack at one moment is the call depth. The total number of calls can be much larger than the deepest depth, because frames are popped and reused. A program whose calls go too deep runs out of stack space: a stack overflow crash.
4 · Visual
The call stack while sq(3) runs. Each box is one stack frame; the top frame belongs to the running function.
Timeline of push/pop for main -> sumsq -> sq(3), sq(4):
push main | push sumsq | push sq(3) | pop sq(3) | push sq(4) | pop sq(4) | pop sumsq | pop main
depth: 1 2 3 2 3 2 1 0
Every call pushes one frame and every return pops it; the depth here never goes above 3.
5 · Worked Examples
Example 1 Easy
What does this print?
void c(void) { printf("c "); }
void b(void) { printf("b1 "); c(); printf("b2 "); }
void a(void) { printf("a1 "); b(); printf("a2 "); }
int main(void)
{
a();
printf("\n");
return 0;
}
Solution
main → a prints a1 and calls b; b prints b1 and calls c; c prints c and returns; b prints b2 and returns; a prints a2.
Output
a1 b1 c b2 a2
Example 2 Medium
What does this print?
void p(int k)
{
printf("%d ", k);
}
void q(int k)
{
p(k + 1);
printf("%d ", k);
p(k + 2);
}
int main(void)
{
q(1);
q(10);
printf("\n");
return 0;
}
Solution
q(1): p(2) prints 2, then 1, then p(3) prints 3. q(10): 11, 10, 12.
Output
2 1 3 11 10 12
Example 3 GATE
f calls g, then h, then g; g calls h twice; h calls nothing. Counting the calls of f, g and h only (not main), how many calls happen in total, and what is the largest number of these frames on the stack at once?
void h(void) { }
void g(void) { h(); h(); }
void f(void) { g(); h(); g(); }
Solution
f: 1 call. g: 2 calls, each making 2 calls of h (4), plus 1 direct call of h from f: 5 calls of h. Total 1 + 2 + 5 = 8. Deepest chain: f → g → h = 3 frames (4 counting main). Instrumenting the program confirms it:
Output
calls=8 deepest=3
6 · GATE Traps
⚠️
- The last function called is the first to return (last in, first out).
- Locals live in the frame and vanish on return; statics and globals do not live on the stack.
- Total calls ≠ maximum depth: frames are popped and pushed again.
- Each call has its own copies of parameters and locals, even when the same function is active more than once.
- Too deep a chain of calls exhausts the stack (stack overflow).
8 · Memory Hook
🧠
“Last plate on, first plate off.” A call adds a plate holding its parameters, locals and the address to go back to; return takes the plate away.
9 · Connect the Dots
Lessons 1.3.1–1.3.3 explained why a function works on copies: each call gets its own frame, which disappears on return. So far a function can never change a variable that lives in its caller’s frame. Next lesson answers: can a program refer to a variable by its memory address, so that other code can reach it?
10 · Self-Test
GATE format. Try every question before opening the vault.
Q1MCQ1 markOrder
main calls A; A calls B; B calls C. In what order do they return?
- (A) C, A, B
- (B) C, B, A
- (C) B, C, A
- (D) A, B, C
Q2MSQ1 markFrames
Which are kept in a function’s stack frame?
- (A) its static local variables
- (B) its automatic local variables
- (C) the return address
- (D) its parameters
Q3MCQ2 marksOutput
What does this print?
void p(int k)
{
printf("%d ", k);
}
void q(int k)
{
p(k + 1);
printf("%d ", k);
p(k + 2);
}
int main(void)
{
q(1);
q(10);
printf("\n");
return 0;
}
- (A)
1 2 3 10 11 12 - (B)
1 2 3 11 10 12 - (C)
2 3 1 11 12 10 - (D)
2 1 3 11 10 12
Q4NAT2 marksDepth
main calls f; f calls g three times; each g calls h once. Counting main, what is the maximum number of frames on the stack at one time?
Numerical answer: ________
Q5NAT1 markCalls
In the same program (f calls g three times; each g calls h once), how many calls of f, g and h happen in total?
Numerical answer: ________
11 · Rule Card
- Call stack: push a frame per call, pop on return; last in, first out.
- Frame = parameters + automatic locals + return address + saved bookkeeping.
- Locals vanish with their frame; statics/globals live outside the stack.
- Depth = frames at one moment; total calls can exceed depth.
- Too deep → stack overflow.
12 · Concept Ledger
Unlocked in this lesson. From here on, these terms may be used.
stack (last in, first out)pushpopcall stackstack frame / activation recordreturn addresscall depthstack overflow
13 · Answer Vault
Answer Vault
Try the questions first. Open when you are ready to check.
Recall Warm-up
- Once, for the whole run.
- −1.
-
Self-Test
Q1 Answer: (B)1 mark
Last in, first out.
- (A) is wrong: B returns before A.
- (C) is wrong: C must finish before B.
- (D) is wrong: A cannot finish while B is running.
Re-read: Lesson 1.3.3 — How calls work: the call stack and stack frames
Q2 Answer: (B) (C) (D)1 mark
Statics live in a fixed area, not in frames.
- (A) is wrong: Statics are outside the stack.
Re-read: Lesson 1.3.3 — How calls work: the call stack and stack frames
Q3 Answer: (D)2 marks
Inside q, p(k + 1) prints first, then k, then p(k + 2).
Output
2 1 3 11 10 12
- (A) is wrong: q prints k after the first call to p.
- (B) is wrong: q(1) prints 2 1 3.
- (C) is wrong: k is printed between the two calls.
Re-read: Lesson 1.3.3 — How calls work: the call stack and stack frames
Q4 Answer: 42 marks
main → f → g → h: 4.
Re-read: Lesson 1.3.3 — How calls work: the call stack and stack frames
Q5 Answer: 71 mark
1 + 3 + 3 = 7.
Re-read: Lesson 1.3.3 — How calls work: the call stack and stack frames