1. Gyanastra
  2. Data Structures
  3. Storage Classes
  4. Automatic Variables and Activation Records in Function Calls
Free lesson, no account needed 6 min read 20 practice questions after it
Asked in past papers15 times
  • GATE CSE202620252024201920152010200820072003200220011992
  • ISRO20172014
  • UGC NET2006

Data Structures · Storage Classes

Automatic Variables and Activation Records in Function Calls

Overview

Automatic (local) variables are created fresh on every function call and live inside that call's activation record on the stack. Understanding this explains why a local counter cannot remember its value between calls, and why a function called four times can still print 1.

Key concepts

  • Automatic (local) variable: created fresh every time a function is called; each call gets its own new copy, placed inside that call's activation record on the stack.
    When the function returns, the activation record is popped off, so the variable's value is destroyed and cannot be retained across calls.
  • Activation record (stack frame): the block created for a function call on the stack.
    It contains all the local variables of that function and the return address — the address in the caller to which control must return after the call finishes.
  • Ignoring a return value: calling a function as a standalone statement without storing its return value is perfectly legal in C.

How it works

  1. main's activation record is created first. main has no local variables, so its frame mainly holds the return address (for simplicity, say address 1, 2, 3, 4). This is the address to resume at after any called function returns.
  2. First call: an activation record for count_function_call is created. Its local variable count gets a copy here and is initialized to 0. count++ makes it 1, and 1 is returned (but not caught).
    The function ends, so this activation record is completely popped off, and main resumes from the next line.
  3. Second call: a fresh activation record is created for the same function, a fresh copy of count is created, set to 0, incremented to 1, returned, and the frame is popped off again.
  4. Third call: exactly the same thing happens — new copy of count = 0, incremented to 1, frame popped.
  5. Fourth call (inside printf): printf is itself a function call, so an activation record is created for printf as well. printf calls count_function_call, which again gets its own fresh copy of count = 0, incremented to 1, and returns 1.
    That 1 is written into printf, which prints it.

Activation records across four calls

Each of the four calls creates a fresh activation record with a new count initialized to 0, so every call returns 1 and the frame is popped on return.

Worked examples

The program calls a function several times and then tries to print how many times it was called.

int count_function_call(void) {
    int count = 0;      // local (automatic) variable, initialized to 0
    count++;            // increment it
    return count;       // return the incremented value
}

int main() {
    count_function_call();   // 1st call
    count_function_call();   // 2nd call
    count_function_call();   // 3rd call
    printf("%d times function is called\n", count_function_call());  // 4th call
    return 0;
}
  • The function is called three times as standalone statements; the returned value is not caught in any variable.
  • The fourth call happens inside printf, so its return value is passed directly as the argument to printf.
  • Each of the four calls creates a fresh count = 0, increments it to 1, and returns 1; each frame is popped off on return.

Output:

1 times function is called

Even though the function was called four times, the printed answer is 1, because count is an automatic variable and cannot retain its value between calls. To retain the value across calls, the variable must live somewhere other than the stack (for example, a static variable).

Comparison

Variable kindDefault (uninitialized) value
Local / automatic variableGarbage value
Global variable0
Static variable0

Revision summary

  • An activation record holds the function's local variables and the return address.
  • When a function returns, its activation record is popped off the stack; all automatic variables in it are destroyed.
  • An uninitialized local variable contains a garbage value; global and static variables are always initialized to 0.
  • printf is also a function call, so it too gets its own activation record.
  • Ignoring a function's return value is legal in C.
  • Each call creates a fresh copy of an automatic variable, so its value is not retained across calls — the program prints 1, not 4.

Previous-year exam insights

  • C uses static (lexical) scoping: a name inside a function refers to the declaration in the nearest enclosing block of the program text, not to the caller's variable; a local declaration in an inner block shadows an outer/global variable of the same name for that block only.
    Asked inGATE 2026GATE 2007GATE 2003
  • In C, function arguments are passed by value: the callee gets copies, so assigning to a parameter never changes the caller's variable; to modify the caller's variable you must pass its address and dereference the pointer.
    Asked inGATE 2015GATE 2010GATE 2008
  • Returning the address of an automatic (local) variable is invalid because its activation record is destroyed on return (dangling pointer); returning a pointer obtained from malloc is safe since that storage is not on the stack.
    Asked inGATE 2001
  • Evaluation order of function arguments and of operands is unspecified in C (except &&, ||, ?: and the comma operator), so side effects such as printf inside arguments can produce implementation-dependent output; the exam states the assumed order explicitly.
    Asked inGATE 2024
  • A function call's activation record typically holds parameters, local variables, saved registers, and the return address; the number of activation records on the stack at any time equals the current depth of the call chain (main plus active calls), which is unbounded in general.
    Asked inGATE 2002
  • Static scoping vs dynamic scoping: under static scoping a non-local name resolves to the enclosing program-text scope (global), while under dynamic scoping it resolves to the most recent active call's binding; call-by-need evaluates an argument at most once, only when first used.
    Asked inGATE 2007GATE 2003
Pages 1–2 of 2