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


Please Help Members By Posting Answers For Below Questions

"%u" unsigned integer print the a) address of variable b) value of variable c) name of a variable d) none of the above

804


How to write a multi-statement macro?

802


What is a program flowchart and explain how does it help in writing a program?

937


Here is a good puzzle: how do you write a program which produces its own source code as output?

816


Explain threaded binary trees?

882






How can I write a function analogous to scanf?

865


Do you know the difference between malloc() and calloc() function?

792


What are the advantages of c language?

833


What is the equivalent code of the following statement in WHILE LOOP format?

1063


What are conditional operators in C?

788


Explain what is the best way to comment out a section of code that contains comments?

903


Function which gives a pointer to a binary trees const an integer value at each code, return function of all the nodes in binary tree.?

795


How do I get a null pointer in my programs?

820


What is #include conio h?

758


Tell us something about keyword 'auto'.

823