Storage-Classes
Question 1 |
An external variable
is globally accessible by all functions | |
has a declaration "extern" associated with it when declared within a function | |
will be initialized to 0 if not initialized | |
All of these |
Question 1 Explanation:
An external variable can be accessed by all the functions in all the modules of a program. It is a global variable. For a function to be able to use the variable, a declaration or the definition of the external variable must lie before the function definition in the source code. Or there must be a declaration of the variable, with the keyword extern, inside the function.
Question 2 |
What will be the output of following?
main()
{
static int a=3;
printf("%d",a--);
if(a)
main();
}
main()
{
static int a=3;
printf("%d",a--);
if(a)
main();
}
3 | |
3 2 1 | |
3 3 3 | |
Program will fall in continuous loop and print 3 |
Question 2 Explanation:
The variable is static variable, the value is retained during multiple function calls. Initial value is 3
“A--” is post decrement so it will print “3”
if(2) condition is true and main() function will call again , Here the “a” value is 2.
“A--” is post decrement so it will print “2”
if(1) condition is true and main() function will call again Here the “a” value is 1.
“A--” is post decrement so it will print “1”
if(0) condition is false it won’t call main() function
“A--” is post decrement so it will print “3”
if(2) condition is true and main() function will call again , Here the “a” value is 2.
“A--” is post decrement so it will print “2”
if(1) condition is true and main() function will call again Here the “a” value is 1.
“A--” is post decrement so it will print “1”
if(0) condition is false it won’t call main() function
Question 3 |
What will be the output of following?
main()
{
static int a=3;
printf("%d",a--);
if(a)
main();
}
main()
{
static int a=3;
printf("%d",a--);
if(a)
main();
}
3 | |
3 2 1 | |
3 3 3 | |
Program will fall in continuous loop and print 3 |
Question 3 Explanation:
Step-1: initial a=3 but we are given storage class is static.
Step-2: In printf( ) we are given post decrement. So, it will print 3 then decrements.
Step-3: if(2) then we are calling main() function, a=2 because of static otherwise it will be 3 only.
Step-4: It will print 2 and next iteration will print 1.
Step-2: In printf( ) we are given post decrement. So, it will print 3 then decrements.
Step-3: if(2) then we are calling main() function, a=2 because of static otherwise it will be 3 only.
Step-4: It will print 2 and next iteration will print 1.
There are 3 questions to complete.