Storage Class II-241
void func(void);
static c=4;
void main()
{
clrscr();
while(c--)
{
fun();
}
}
OUTPUT:
i is 1 and count is 3
i is 2 and count is 2
i is 3 and count is 1
i is 4 and count is 0
b. Program with auto keyword
void func(void);
int c=4;
void main()
{
clrscr();
while(c--)
{
fun();
}
}
fun( void )
{
auto int i=0;
i++;
printf("i is %d and count is %d ", i, c);
}
OUTPUT:
i is 1 and count is 3
i is 1 and count is 2
i is 1 and count is 1
i is 1 and count is 0
Explanation: Due to auto class, the value of variable does not retain between calling function; hence,
the value of i remains 1 throughout the different calling functions.
M11_ITL-ESL4791_02_SE_C11.indd 241 12/22/2012 5:05:15 PM
II-242 Programming Concepts
18. Why register storage class does not support all data types?
Ans: The CPU registers 8086 in microcomputer are 16-bit registers. The data types float and double
need more than 16 bits space. If we define variables of these data type with register class, no errors
will be shown. The compiler treats them as a variable of auto class.
19. Can we declare a variable in different scopes with different data types? Answer in short.
Yes, we can declare the variable in different scopes with different data types except for some data types
with register scope or storage class.
20. Explain lifetime and visibility of a variable.
Ans:
Lifetime of a variable:  Every variable has its lifetime, i.e., its time duration during which its status is
active in the program. We can also say that lifetime of a variable is the time gap between its declaration
and cleanup. The lifetime depends upon the storage class. For example, auto variable gets destroyed
immediately when the function execution is over, whereas static variable remains in the memory.
Visibility of a variable: It is another property. It defines its scope. The scope is of two types, i.e., local
and global. Global is recognized throughout the program, whereas the local variable scope is limited to
the declaration block.
Multiple-choice Questions
1. A static variable is one that .
(a) Retains its value throughout the life of the program
(b) Cannot be initialized
(c) Is initialized once at the commencement of the execution and cannot be changed at the run time
(d) Is the same as an automatic variable, but is placed at the head of the program
2. An external variable is one .
(a) That is globally accessible by all functions
(b) That is declared outside the body of any function
(c) That resides in the memory till the end of the program
(d) All of the above
3. If a storage class is not mentioned in the declaration, then default storage class is .
(a) auto (b) static (c) extern (d) register
4. If the CPU fails to keep the variables in CPU registers, in that case the variables are assumed
.
(a) auto (b) static (c) extern (d) None of the above
5. What will be the value of variable x on execution of the following program?
int x;
void main()
{
clrscr();
x++;
printf(" %d",x);
}
M11_ITL-ESL4791_02_SE_C11.indd 242 12/22/2012 5:05:15 PM
Storage Class II-243
(a) x = 1 (b) I = 0 (c) Garbage value (d) None of the above
6. Which variables of following storage class are stored in RAM?
(a) auto (b) register (c) None of the above
7. Which variables of the following storage class are stored in CPU registers?
(a) auto (b) register (c) None of the above
8. Which of the following is not C keyword?
(a) automatic (b) register (c) static (d) extern
9. Which of the following storage class is faster?
(a) auto (b) register (c) static (d) extern
10. Which of the following storage class provides garbage value when variables are not initialized?
(a) auto (b) static (c) extern
11. The basic task of storage class is to determine the variable’s .
(a) Scope (b) Default value (c) Storage (d) All of the above
12. What is the output of the following program?
static x=2;
void main()
{
printf("%d",++x);
}
(a) 2 (b) 3
(c) 1 (d) None of the above
13. What is the output of the following program?
void main()
{
extern x
printf("%d",++x);
}
(a) 0 (b) Garbage value (c) Syntax error
14. What is the output of the following program?
int x;
void main()
{
extern x;
printf("%d",++x);
}
(a) 1 (b) Garbage value (c) Syntax error
M11_ITL-ESL4791_02_SE_C11.indd 243 12/22/2012 5:05:15 PM
II-244 Programming Concepts
15. What is the output of the following program?
int x=5;
void main()
{
extern x;
printf("%d",x++);
}
(a) 1 (b) Garbage value
(c) 5 (d) None of the above
16. The scope of static variables is the same as that of .
(a) auto (b) static
(c) extern (d) None of the above
17. The scope of extern variables is .
(a) Local (b) Global
(c) Inside the function (d) None of the above
18. Even if not initialized, which of the following type of variable dose not hold garbage value?
(a) Auto variable (b) External variable (c) Register variable
19. The auto variable can be of .
(a) Static (b) External (c) Pointer
20. The storage class of the variable tells the compiler .
(a) Storage area of variable
(b) The scope of the variable
(c) Both (a) and (b)
21. If external and auto variables are declared with the same name, priority is given to .
(a) Auto variable (b) External variable (c) None of them
22. Which of the following keyword is not related to C storage classes?
(a) static (b) auto (c) case
Answers
1. (a) 2. (a) 3. (a) 4. (a) 5. (a) 6. (a) 7. (b) 8. (a) 9. (b) 10. (a)
11. (d) 12. (b) 13. (c) 14. (a) 15. (c) 16. (a) 17. (b) 18. (b) 19. (a) 20. (c)
21. (a) 22. (c)
True or False
1. Auto variables are defined inside the function.
2. Auto variable must be prefixed with keyword auto.
3. Auto functions are defined on the stack.
4. Auto variables are global variables.
M11_ITL-ESL4791_02_SE_C11.indd 244 12/22/2012 5:05:15 PM
Storage Class II-245
5. The external variables are available to the entire program.
6. The external variables are global in access.
7. External variables are defined outside the function.
8. In case both external and auto variables are defined with same name, external variables get
identified (assume only main() function exists).
9. Static variable can be of external and auto types.
10. The value of static variables persists.
11. A static variable must be initialized explicitly.
12. We can place value of variable in CPU register using the keyword register.
13. We can confirm if the values are placed or not in CPU register.
14. If register variables are not kept in the CPU register, they are treated as auto variables.
Answers
1. True 2. False 3. True 4. False 5. True 6. True 7. True 8. False 9. True 10. True
11. False 12. True 13. False 14. True
Additional Questions
What will be the output of the following programs?
1.
#include <stdio.h>
#include <conio.h>
void main()
{
int v=5;
clrscr();
{
int v=4;
printf(" %d ",v);
}
printf(" %d ",v);
{
int v=9;
printf(" %d ",v);
}
printf(" %d ",v);
}
OUTPUT:
4 5 9 5
M11_ITL-ESL4791_02_SE_C11.indd 245 12/22/2012 5:05:16 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset