Open In App

Problem Solving on Storage Classes and Scoping of Variables

Improve
Improve
Like Article
Like
Save
Share
Report

Storage class of variables includes the scope, visibility and life-time which help to trace the existence of a particular variable during the runtime of a program. There exist four types of storage classes in C: auto, register, static and extern. 

Auto and Register Storage Classes –

  • The scope of variable declared using auto or register storage class is the function or block in which variable is declared.
  • The visibility of variable is the function or block in which variable is declared.
  • The life-time of variable is the time between function/block starts executing till function/block is terminated.
  • The memory for variable in auto variables is allocated on stack.
  • Auto variable can be declared as auto int i or int i. It means default storage class of a variable declared in a function is auto.
  • Register variable can be declared as register int i. However, if register is not available, it will be given auto storage class by compiler.
  • If auto or register variable is not initialized, it contains garbage value.

Static Storage Classes –

  • The scope of variable declared using static storage class is the function or block in which variable is declared.
  • The visibility of variable is the function or block in which variable is declared.
  • The static variable remains in memory until the program is terminated. Therefore, static variable is initialized only once and value of static variable is maintained during function call.
  • The memory for auto variables is allotted in heap and memory for static variables is allotted in data segment. Please refer Memory Layout of C Programs for details.
  • Static variable can be declared as static int i.
  • If static variable is not initialized while declaration, it is initialized by default value e.g.; 0 in case of integer.

Extern Storage Classes –

  • Extern storage class is used to extend the visibility of variables where variable is defined somewhere else (in same file or different file) in the program.
  • Extern variable can be declared as extern int i. However, memory is not assigned to i as it is referencing to another variable i which is defined somewhere else in the program.

Local/Global Scope – A variable declared inside a function or a block has its scope only in that function/block. However, a variable declared outside function has global scope which can be accessed by any function or block. 

Note:

  • If a function has local/static variable with same name as global variable, local/static value is used.
  • If a function does not find a required variable in local scope, it searches the variable in global scope. If it does not find even in global scope, it will throw an error.

Que – 1. Which of the following statement about storage classes is incorrect? (A) A variable with auto storage class has local scope in the function in which it is declared. (B) A variable with static storage class has local scope in the function in which it is declared. (C) A variable declaration with register storage class will return an error if register in CPU is not available. (D) None 

Solution: Option A and B are correct as auto as well as static storage class has local scope. However, option C is incorrect as register storage class is converted to auto storage class if register is not available and it will give warning not error. 

Que – 2. The value of j at the end of the execution of the following C program. (GATE CS 2000)

int incr (int i)
{
   static int count = 0;
   count = count + i;
   return (count);
}
main ()
{
   int i, j;
   for (i = 0; i <=4; i++)
      j = incr(i);
}

(A) 10 (B) 4 (C) 6 (D) 7 

Solution: The for loop is executed as: For i = 0, count will be allocated memory in heap and initialized to 0. The statement count = count + i will make count = 0 + 0 = 0. For i = 1, previous value of count is used. The statement count = count + i will make count = 0 + 1 = 1. For i = 2, previous value of count is used. The statement count = count + i will make count = 1 + 2 = 3. Similarly, 3 and 4 will be added to count variable for i = 3 and 4 respectively. So, answer will be 3 + 3 + 4 = 10. 

Que – 3. Consider the following C program

int a, b, c = 0;
void prtFun (void);
int 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? (A)

3 1
4 1
4 2

(B)

4 2
6 1
6 1

(C)

4 2
6 2
2 0

(D)

3 1
5 2
5 2

Solution: The program is executed as: Firstly, global variables a, b and c will be initialized to 0. After calling of main, static variable a in main will be initialized to 1. When prtFun() is called first time, static variable a is initialized to 2 and local variable b is initialized to 1. The statement a+=++b can be broken into ++b followed by a = a+b. Therefore, b will be incremented to 2. Also, value be a will be 2+2 = 4. Therefore, print statement will print 4, 2. After returning from function, variable b will be destroyed (local scope) and a’s value will be preserved. After returning from first ptrFun(), statement a+=1 will be executed and static variable a in main will be incremented to 2. When prtFun() is called second time, local variable b is initialized to 1. The statement a+=++b can be broken into ++b followed by a = a+b. Therefore, b will be incremented to 2. Also, value be a will be = 4+2 = 6. Therefore, print statement will print 6, 2. After returning from second prtFun(), the main function has static variable a with value 2 and global variable b with value 0. Therefore, 2, 0 is printed. Hence, the answer is (C). 

Que – 4. Consider the C function given below.

int f(int j)
{
  static int i = 50;
  int k;
  if (i == j)
  {
    printf("something");
    k = f(i);
    return 0;
  }
  else return 0;
}

Which one of the following is TRUE? (A) The function returns 0 for all values of j. (B) The function prints the string something for all values of j. (C) The function returns 0 when j = 50. (D) The function will exhaust the runtime stack or run into an infinite loop when j = 50 

Solution: When j is any value other than 50, if condition will fail and 0 will be returned. When j is 50, if condition will be true and function will be called again and again. It may exhaust runtime stack or run into infinite loop. Option (D) is correct. 

Que – 5. Consider the C program shown below.

# include <stdio.h> 
# define print(x)  printf ("%d", x) 
int x; 
void Q(int z) 
{ 
  z += x;
  print(z); 
} 
void P(int *y) 
{ 
  int x = *y+2; 
  Q(x); 
  *y = x-1; 
  print(x);
} 
 
main(void) 
{ 
  x = 5; 
  P(&x); 
  print(x); 
  getchar();
} 

The output of this program is: (GATE CS 2003) (A) 12 7 6 (B) 22 12 11 (C) 14 6 6 (D) 7 6 6 

Solution: The variable x is declared outside main() which has global scope. Also, it is declared inside P() as well. Therefore, P() will use x local to P(). First main() will change the global x to 5. The function P() is called by passing address of global x in y. Therefore, y contains the address of global x. A new variable x is declared in P and initialized to *y+2 = 5+2 = 7. Therefore, local x of P() has value 7. The function Q() is called by passing local x of P() in z. Therefore, z contains value 7. The statement z +=x will add value of global x to z. Therefore, z =z + x = 7+5 =12 will be printed. After returning from Q(), the statement *y = x-1 will be executed. As local x has value 7 and y refers to global x, value at global x will be 7-1 = 6. However, print(x) will print local x. therefore, 7 will be printed. After returning from P(), the main function will print x (global value of x) which is 6. Therefore, 6 will be printed. So, the output is (A).


Last Updated : 23 May, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads