High yield ●●●

🧠 Storage Classes, Scope & Memory

Why it matters: static variables and scope are a favourite way to make a short program tricky; memory-layout questions also overlap with OS and compilers.

Subtopics

Each subtopic has its own page: idea, why, how, example, play, check. Mark each one understood when it clicks. 0 of 5 done.

Formulas

static local
initialised once; lifetime = whole program; scope = its block
Default values
static and global: 0; auto: indeterminate (garbage)
Where does it live?
local → stack; malloc → heap; global/static → data or BSS; code → text
Frame contents
parameters + return address + saved registers + locals
Leak vs dangling
leak = lost pointer, block never freed; dangling = pointer to freed or dead memory

What GATE asks from this

Question types seen repeatedly in past papers. More stars means more frequent.

★★★
Value printed by a program with static counters
Keep the static variable in its own column and update it across calls.
★★★
Shadowing of globals and locals
Match each name to the nearest declaration; blocks create new variables.
★★★
What problem does this malloc code have?
Look for a pointer overwritten before free (leak) or used after free (dangling).

Ratings are approximate, based on recurring patterns in past GATE CSE papers.

Previous year questions

8 real questions from the official GATE CSE papers (2012 to 2023), newest first, each with the official answer key and a worked solution. Questions are the property of the GATE organising institutes; this copy is for your personal study.

8 questions · 0 correct of 0 attempted · marks 0 / 14

Year:
GATE 2023 · Q35NAT1 mark
The integer value printed by the ANSI-C program given below is ______.
#include<stdio.h>
int funcp(){
  static int x = 1;
  x++;
  return x;
}
int main(){
  int x,y;
  x = funcp();
  y = funcp()+x;
  printf("%d\n", (x+y));
  return 0;
}
GATE 2020 · Q46NAT2 marks
Consider the following C functions.
int fun1(int n) {            int fun2(int n) {
  static int i = 0;             static int i = 0;
  if (n > 0) {                  if (n > 0) {
    ++i;                          i = i + fun1(n);
    fun1(n-1);                    fun2(n-1);
  }                             }
  return(i);                    return(i);
}                             }
The return value of fun2(5) is ______.
GATE 2019 Set 2 · Q27MCQ2 marks
Consider the following C program:
#include <stdio.h>
int r(){
  static int num=7;
  return num--;
}

int main(){
  for (r();r();r())
    printf("%d",r());
  return 0;
}
Which one of the following values will be displayed on execution of the programs?
GATE 2017 Set 1 · Q13MCQ1 mark
Consider the following C code:
#include <stdio.h>
int *assignval (int *x, int val) {
  *x = val;
  return x;
}
void main () {
  int *x = malloc(sizeof(int));
  if(NULL == x) return;
  x = assignval(x,0);
  if(x) {
    x = (int *)malloc(sizeof(int));
    if(NULL == x) return;
    x = assignval(x,10);
  }
  printf("%d\n", *x);
  free(x);
}
The code suffers from which one of the following problems:
GATE 2017 Set 1 · Q55NAT2 marks
The output of executing the following C program is ______.
#include <stdio.h>

int total(int v) {
  static int count = 0;
  while(v) {
    count += v&1;
    v >>= 1;
  }
  return count;
}

void main() {
  static int x=0;
  int i=5;
  for(; i>0; i--) {
    x = x + total(i);
  }
  printf("%d\n", x);
}
GATE 2016 Set 1 · Q35MCQ2 marks
What will be the output of the following C program?
void count(int n){
  static int d=1;
  printf("%d ", n);
  printf("%d ", d);
  d++;
  if(n>1) count(n-1);
  printf("%d ", d);
}
void main(){
  count(3);
}
GATE 2012 Code A · Q48MCQ2 marks
Common data for Questions 48 and 49. Consider the following C code segment.
int a, b, c = 0;
void prtFun(void);
main( )
{
  static int a = 1;      /* Line 1 */
  prtFun( );
  a += 1;
  prtFun( );
  printf(" \n %d  %d ", a, b);
}

void prtFun(void)
{
  static int a = 2;      /* Line 2 */
  int b = 1;
  a += ++b;
  printf(" \n %d  %d ", a, b);
}
What output will be generated by the given code segment? (Each option lists three lines.)
GATE 2012 Code A · Q49MCQ2 marks
With the same code as Question 48, what output will be generated if Line 1 is replaced by auto int a = 1; and Line 2 is replaced by register int a = 2;?

Common traps

A static local is initialised only once, not on every call.
Inside a block, static int a = 1; in main and a global a are different variables.
register and auto locals reset on each call.
Returning the address of a local gives a dangling pointer.