Problem Solving on Storage Classes and Scoping of Variables
Last Updated :
23 May, 2022
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).
Similar Reads
Storage Classes in C++ with Examples
C++ Storage Classes are used to describe the characteristics of a variable/function. It determines the lifetime, visibility, default value, and storage location which helps us to trace the existence of a particular variable during the runtime of a program. Storage class specifiers are used to specif
7 min read
Perl | Scope of Variables
The scope of a variable is the part of the program where the variable is accessible. A scope is also termed as the visibility of the variables in a program. In Perl, we can declare either Global variables or Private variables. Private variables are also known as lexical variables. Scope of Global Va
6 min read
How to Access Global Variable if there is a Local Variable with Same Name in C/ C++?
Local Variable: The variable whose scope lies inside a function or a block in which they are declared. Global Variable: The variable that exists outside of all functions. It is the variable that is visible from all other scopes. We can access global variable if there is a local variable with same na
2 min read
Storage Classes in C
In C, storage classes define the lifetime, scope, and visibility of variables. They specify where a variable is stored, how long its value is retained, and how it can be accessed which help us to trace the existence of a particular variable during the runtime of a program. In C, there are four prima
4 min read
Understanding Classes and Objects in Java
The term Object-Oriented explains the concept of organizing the software as a combination of different types of objects that incorporate both data and behavior. Hence, Object-oriented programming(OOPs) is a programming model, that simplifies software development and maintenance by providing some rul
10 min read
Output of C++ programs | Set 41 (Structure and Union)
Prerequisite: Structures and Union QUE.1 What will be the output of this code? #include <iostream> using namespace std; typedef struct s1 { int a; float b; union g1 { char c; double d; }; } new1; int main() { cout << sizeof(new1); return 0; } (a)17 (b)16 (c)8 (d)9 Output: (c) 8 Explanati
3 min read
Parent and Child Classes Having Same Data Member in Java
In C++ we have all class member methods as non-virtual. In order to make so, we have to use a keyword as a prefix known as virtual. Whereas in Java, we have all class member methods as virtual by default. In order to make them non-virtual, we use the keyword final. Reference variables in Java are ba
3 min read
C# | Types of Variables
A variable is a name given to a memory location and all the operations done on the variable effects that memory location. In C#, all the variables must be declared before they can be used. It is the basic unit of storage in a program. The value stored in a variable can be changed during program exec
10 min read
Difference between Identifiers and Variables in C
Perquisites: Identifiers, Variables Identifiers Identifiers are used for the naming of variables, functions, and arrays. It is a string of alphanumeric characters that begins with an alphabet, or an underscore( _ ) that are used for variables, functions, arrays, structures, unions, and so on. It is
2 min read
Internal static variable vs. External static variable with Examples in C
The static variable may be internal or external depending on the place of declaration. Both kinds of static variables are stored in initialized data segments. Internal Static Variables Internal Static variables are defined as those having static variables which are declared inside a function and ext
2 min read