void main()
{
static int i = 5;
if(--i)
{
main();
printf("%d
",i);
}
}
what would be output of the above program and justify your
answer?
}
Answer Posted / samrat
Ans: 0 0 0 0
The first thing you have to remember is that static
variables are initialized only once. The second thing is
that static variables have a life time scope and they retain
their value between function calls.
"i" is first initialized to 5. in the if condition the value
of i is changed to 4. main() is called again and the value
of i is changed to 3 in the if condition and main is called
again. Now the value of i is changed to 2 and main is called
again. Now the value of i is changed to 1 and main is called
again. After this the value of i is changed to "0" and the
block is excited.
As the value of i is now "0", it is printed 4 times for each
of the calls for main(). So the ans will be
0
0
0
0
Thanks,
Samrat
| Is This Answer Correct ? | 67 Yes | 5 No |
Post New Answer View All Answers
Write a C/C++ program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the maximum number of concurrent threads that the InnoDB plug-in can create.
What is pre-emptive data structure and explain it with example?
A collection of data with a given structure for excepting storing and providing on demand data for multiple users a) linked list b) datastructer c) database d) preprocessor
What is variable and explain rules to declare variable in c?
What is a scope resolution operator in c?
What is calloc malloc realloc in c?
What is header file in c?
Write a program to print factorial of given number without using recursion?
How does selection sort work in c?
Explain null pointer.
What are the 4 types of organizational structures?
How do you define a function?
Can we change the value of constant variable in c?
What is indirection in c?
How can I pad a string to a known length?