ACompressed recap
🧾
A function is defined once (type name(params) { … }), declared before use with a prototype, and called with arguments whose values are copied into the parameters (call by value). return ends it and hands back one value.
Scope is where a name is visible; lifetime is when it exists. Locals live in their block and start with garbage; globals and statics live for the whole run and start at 0; a static local keeps its value between calls; inner declarations shadow outer ones; extern declares without defining.
Each call pushes a stack frame (parameters, locals, return address) on the call stack and pops it on return, last in first out.
BMerged rule card
1.3.1 Declaring, defining, calling; parameters; return values; call by value
- type name(params) { … return v; } — void when nothing is returned.
- Call: evaluate args → copy into params → run → value replaces the call.
- Prototype (declaration) before first use; definition = body.
- Call by value only: parameters are copies; caller’s variables unchanged.
- return ends the function immediately.
- Argument evaluation order is unspecified.
1.3.2 Scope vs lifetime; local, global, static, extern, register; shadowing in blocks; static local variables
- Scope = where a name is visible; lifetime = when its memory exists.
- Local/auto: block scope, born and destroyed with the block, garbage start.
- Global: file scope (extern elsewhere), whole run, starts at 0.
- static local: block scope, whole run, initialized once (0 default), keeps value.
- static global/function: this file only. extern: declaration, not definition.
- Inner declarations shadow outer ones inside their block only.
- register: hint; its address may not be taken.
1.3.3 How calls work: the call stack and stack frames
- 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.
CInterleaved GATE-style questions
Q1MCQ2 marksStatic and global
What does this print?
int x = 1;
int f(void)
{
static int y = 2;
int z = 3;
x += y;
y *= 2;
z++;
return x + y + z;
}
int main(void)
{
int a = f();
int b = f();
printf("%d %d %d\n", a, b, x);
return 0;
}
- (A)
9 19 7 - (B)
11 19 7 - (C)
11 19 3 - (D)
11 15 7
Q2MCQ1 markCall by value
Which statement is true about void f(int x) { x = x + 1; } called as f(a);?
- (A) a becomes x
- (B) the program does not compile
- (C) a is unchanged
- (D) a increases by 1
Q3NAT1 markStatic
A function contains static int n = 3; n += 2; return n;. What does its fourth call return?
Numerical answer: ________
Q4MCQ2 marksShadowing
What does this print?
int x = 1; /* global */
int main(void)
{
printf("%d ", x);
int x = 2; /* local x shadows the global x */
printf("%d ", x);
{
int x = 3; /* block x shadows the local x */
printf("%d ", x);
}
printf("%d\n", x);
return 0;
}
- (A)
1 2 3 3 - (B)
1 1 3 1 - (C)
1 2 3 2 - (D)
2 2 3 2
Q5MCQ1 markStack
Which is stored in a stack frame but not in the static area?
- (A) automatic locals
- (B) global variables
- (C) static locals
- (D) string literals
Q6NAT2 marksOutput
What does this print?
int h(int a, int b)
{
return a * 10 + b;
}
int main(void)
{
printf("%d\n", h(h(1, 2), h(3, 4)));
return 0;
}
Numerical answer: ________
Q7MSQ2 marksScope and lifetime
Which are correct?
- (A) A global variable can be used in a function defined above its declaration without any extra declaration.
- (B) Two functions can each have a local variable named i without conflict.
- (C) extern int k; tells the compiler k is defined somewhere else.
- (D) A parameter is a local variable of its function.
Q8NAT1 markDepth
main calls a; a calls b twice; b calls c once. Counting main, what is the maximum number of frames on the stack at once?
Numerical answer: ________
Q9MCQ1 markOrder
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;
}
- (A)
a1 b1 c b2 a2 - (B)
c b1 b2 a1 a2 - (C)
a1 b1 b2 c a2 - (D)
a1 a2 b1 b2 c
DAnswer Vault
Answer Vault
Try the questions first. Open when you are ready to check.
Q1 Answer: (B)2 marks
Call 1: x = 1 + 2 = 3, y = 4, z = 4 → 3 + 4 + 4 = 11. Call 2: x = 3 + 4 = 7, y = 8, z = 4 (fresh local) → 7 + 8 + 4 = 19. x ends at 7.
Output
11 19 7
- (A) is wrong: z is incremented to 4 in the first call too.
- (C) is wrong: x is global and keeps growing: 7.
- (D) is wrong: y doubles to 8 in the second call.
Re-read: Lesson 1.3.2 — Scope vs lifetime; local, global, static, extern, register; shadowing in blocks; static local variables
Q2 Answer: (C)1 mark
x is a copy of a.
- (A) is wrong: Nothing is copied back.
- (B) is wrong: It is valid C.
- (D) is wrong: Only the copy changes.
Re-read: Lesson 1.3.1 — Declaring, defining, calling; parameters; return values; call by value
Q3 Answer: 111 mark
5, 7, 9, 11.
Re-read: Lesson 1.3.2 — Scope vs lifetime; local, global, static, extern, register; shadowing in blocks; static local variables
Q4 Answer: (C)2 marks
The block x disappears at its closing brace.
Output
1 2 3 2
- (A) is wrong: The inner x is gone after the block.
- (B) is wrong: The local x is 2.
- (D) is wrong: Before the local is declared, the global (1) is visible.
Re-read: Lesson 1.3.2 — Scope vs lifetime; local, global, static, extern, register; shadowing in blocks; static local variables
Q5 Answer: (A)1 mark
Automatic locals belong to their call’s frame.
- (B) is wrong: Globals live in the static area.
- (C) is wrong: Statics live in the static area.
- (D) is wrong: Literals are fixed data, not frames.
Re-read: Lesson 1.3.3 — How calls work: the call stack and stack frames
Q6 Answer: 1542 marks
154.
Output
154
Re-read: Lesson 1.3.1 — Declaring, defining, calling; parameters; return values; call by value
Q7 Answer: (B) (C) (D)2 marks
A global is visible only after its declaration (or after an extern declaration).
- (A) is wrong: Scope starts at the declaration.
Re-read: Lesson 1.3.2 — Scope vs lifetime; local, global, static, extern, register; shadowing in blocks; static local variables
Q8 Answer: 41 mark
main → a → b → c: 4.
Re-read: Lesson 1.3.3 — How calls work: the call stack and stack frames
Q9 Answer: (A)1 mark
Each caller resumes only after its callee finishes.
Output
a1 b1 c b2 a2
- (B) is wrong: a1 prints before b is called.
- (C) is wrong: c runs before b2.
- (D) is wrong: a2 prints after b returns.
Re-read: Lesson 1.3.3 — How calls work: the call stack and stack frames