void main()
{
static int i = 5;
if(--i)
{
main();
printf("%d
",i);
}
}

what would be output of the above program and justify your
answer?

}

Answers were Sorted based on User's Feedback



void main() { static int i = 5; if(--i) { main(); printf("%d ",i); } } w..

Answer / 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

void main() { static int i = 5; if(--i) { main(); printf("%d ",i); } } w..

Answer / srsabariselvan

0
0
0
0

static variable's value is stored in memory statically upto
end of the program. so if the variable comes out of the
function it retains its value

Is This Answer Correct ?    13 Yes 5 No

void main() { static int i = 5; if(--i) { main(); printf("%d ",i); } } w..

Answer / sri ram

This prog'll not produce any output since the value of i
reduces when it reaches zero if block will not be executed
and the program is terminated....

Is This Answer Correct ?    7 Yes 16 No

void main() { static int i = 5; if(--i) { main(); printf("%d ",i); } } w..

Answer / senthil kumar.s

ans:0

Is This Answer Correct ?    1 Yes 13 No

void main() { static int i = 5; if(--i) { main(); printf("%d ",i); } } w..

Answer / biren

we can't call a main function with in main.

Is This Answer Correct ?    7 Yes 32 No

Post New Answer

More C Interview Questions

what defference between c and c++ ?

6 Answers  


Why doesnt long int work?

0 Answers  


Define a structure to store roll no, name and marks of a student. Using the structure of above write a ‘C’ program to create a file “student.dat”. There must be one record for every student in the file. Accept the data from the user.

0 Answers  


What is queue in c?

0 Answers  


Explain with the aid of an example why arrays of structures don’t provide an efficient representation when it comes to adding and deleting records internal to the array.

0 Answers  






a character variable can at a time store a) 1 character b) 8 characters c) 254 characters d) none of the above

3 Answers  


How do we declare variables in c?

0 Answers  


A function can make the value of a variable available to another by a) declaring the variable as global variable b) Passing the variable as a parameter to the second function c) Either of the two methods in (A) and (B) d) binary stream

0 Answers  


Explain what is the benefit of using an enum rather than a #define constant?

0 Answers  


The postoder traversal is 7,14,3,55,22,5,17 Then ur Inorder traversal is??? please help me on this

1 Answers  


Print the foll in C...eg when n=5 the o/p must b + + + + + + + + + + + + + + + + +

1 Answers  


What do you know about the use of bit field?

0 Answers  


Categories