C-Programming Fun and Storage Class DPP-1
C-Programming Fun and Storage Class DPP-1
DPP - 01
1. Consider the following C-code. What is f(n-1);
the value returned by fun (10)? }
int fun (int n) { n++;
static int i=10; printf("%d\t", n);
if (n>=200) return (n+i); }
else{ The sum of the values printed when f(5) is
n=n+i; called is
i=n+i
return fun(n); 4. Which of the following is the correct output for
} the code given below ?
} main()
A. 890 B. 210 {
C. 340 D. None of these void fun();
fun();
2. Consider the following program: fun();
#include<stdio.h> }
static int i; void fun()
extern int i=3; {
void f(int n) static int i =2;
{ auto int j = 6;
if(n) printf(“%d”, i++);
{ printf(“%d”, j++);
printf("%d\t", i++); }
f(n-1); A. 2 6 3 B. 2 6 3 6 4
} C. 2 6 3 6 D. 2 6 3 7
}
int main() 5. Consider the following function:
{ int func(int n)
f(4); {
return 0; static int k=0;
} if(n>0){
The output is: k++;
(a) 3 4 5 6 7 (b) 3 4 5 6 return 2*func(n/2)+k;
(c) 0 1 2 3 (d) Compilation error. }
return n+k--;
3. Consider the following function: }
void f(int n){ The value returned by func(8) is
static int i=3;
if(n%2){
n+=i--;
printf("%d\t", n);
C-PROGRAMMING (FUN & STORAGE)
DPP - 01
6. #include<stdio.h> 9. Consider the following code
void main(){ #include <stdio.h>
extern int a; int i=1;
extern int a; int f(){
extern int a; static int i=2;
printf("%d", a); return i++;
} }
int a = 15; int main()
The output is- {
(a) Garbage value extern int i;
(b) Compilation error char a='B';
(c) 15 printf("%d",a+f()+f()+i);
(d) No output return 0;
}
7. Which of the following statement(s) is/are The output is
CORRECT?
(a) A static variable has internal linkage.
(b) Static variables are stored in the data
segment.
(c) Auto variables are stored in the heap
segment.
(d) Register variables behave as auto variables
by default.
DPP - 01
Answer Key
1. Answer : A
2. Answer : B
3. Answer : 45
4. Answer : c
5. Answer : 109
6. Answer : C
7. Answer : (a, b, d)
8. Answer : C
9. Answer : 72
C-PROGRAMMING (FUN & STORAGE)
DPP - 01
HINTS&SOLUTIONS 3. Solution: 45
1. Solution : 4. Solution:C
Static variable is initialized only once and it will Auto storage class is reinitialized at every
retain the value in between the function calls. On function call whereas static storage class
the last iteration n &i will become 340 and 550 persists its value between different function
respectively and so it will return 340+550 = 890. calls.
When the function fun() is called for the first
2. Solution:B time , value of i and j are printed and
extern int i=3; //No compilation error. Assigns 3 sequentially incremented. Output after first
to time fun() called : 2 6
global static variable i. During the second function call i is incremented
f(4): whereas j is reinitialized.
if(4){//true
printf("%d\t", i++);//3 is printed. Global static i 5. Solution :109
is func(8):
incremented to 4. static int k=0;
f(n-1);//f(3) is called if(n>0){//8>0 is TRUE
} k++; //static k is incremented to 1.
f(3): return 2*func(n/2)+k;//func(4) is called.
if(3){//true Returns (2*53+3) i.e 109
printf("%d\t", i++);//4 is printed. Global static i }
is func(4):
incremented to 5. static int k=0;
f(n-1);//f(2) is called if(n>0){//4>0 is TRUE
} k++; //static k is incremented to 2.
f(2): return 2*func(n/2)+k;//func(2) is called.
if(2){//true Returns (2*25+3) i.e 53
printf("%d\t", i++);//5 is printed. Global static i }
is func(2):
incremented to 6. static int k=0;
f(n-1);//f(1) is called if(n>0){//2>0 is TRUE
} k++; //static k is incremented to 3.
f(1): return 2*func(n/2)+k;//func(1) is called.
if(1){//true Returns (2*11+3) i.e 25
printf("%d\t", i++);//6 is printed. Global static i }
is func(1):
incremented to 7. static int k=0;
f(n-1);//f(0) is called. It does nothing. if(n>0){//1>0 is TRUE
} k++; //static k is incremented to 4.
Output: 3 4 5 6 return 2*func(n/2)+k;//func(0) is called.
C-PROGRAMMING (FUN & STORAGE)
DPP - 01
Returns(2*4+3) i.e 11 9. Solution:72
} main(): extern int i; //extern variable shares the
f(0) returns (0+4). Static k is decremented space of global variables. char a='B';
to 3 printf("%d",a+f()+f()+i);//(66+2+3+1) i.e. 72 is
printed return 0;
6. Solution:C
‘extern int a’ can be written multiple times.
extern shares the space of global variables.
Output: 15
7. Solution : (a, b, d)
(a) CORRECT. A static variable has internal
linkage.
(b) CORRECT. Static variables are stored in the
data segment.
(c) INCORRECT. Auto variables are stored in the
stack segment.
(d) CORRECT. Register variables behave as auto
variables by default.
8. Solution:C
static int j;
static int j=3;
Multiple declarations of global static variables
are
allowed. Hence, no compilation error.
f():
auto int i=2; // i is an auto or local variable.
return i+++--j; // return 2+2 i.e. 4. Auto i is
incremented to 3, static j contains 2. f():
auto int i=2; // i is an auto or local variable. It
will be re-initialized to 2 return i+++--j; //
return 2+1 i.e. 3. Auto i is incremented to 3,
static j contains main(): char
a='B';printf("%d",a+f()+f());
// (66+4+3) i.e 73 is printed. Output: 73
9. Solution
f(): static int i=2; return i++; // return 2; i is
incremented to 3. f():
static int i=2; //i contains 3.
return i++; // return 3; i is incremented to