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

Write a program to find factorial of a number using recursive function.

0 Answers   Global Logic, TCS,


What is void pointers in c?

0 Answers  


What is the meaning of c in c language?

0 Answers  


write a Program to dispaly upto 100 prime numbers(without using Arrays,Pointer)

26 Answers   ADITI, iFlex, Infosys, Oracle, TCS, Unicops, Wipro,


how to find the largest element of array without using relational operater?

6 Answers   Satyam, Wipro,






Write a program of prime number using recursion.

0 Answers   Aspiring Minds,


5) Write a program that takes a 3 digit number n and finds out whether the number 2^n + 1 is prime, or if it is not prime find out its factors.without using big int and exponential function

1 Answers   TCS,


What is the proper way of these job Tell me about there full work

0 Answers   EDS,


difference between ordinary variable and pointer in C?

2 Answers  


main() { int i,j,A; for(A=-1;A<=1;A++) prinf("%d\t",!!A); }

6 Answers  


what is structuer?

4 Answers   LG Soft, Wipro,


Write a C program that defines a 2-dimentional integer array called A [50][50]. Then the elements of this array should randomly be initialized either to 1 or 0. The program should then print out all the elements in the diagonal (i.e. a[0][0], a[1][1],a[2][2], a[3][3], ……..a[49][49]). Finally, print out how many zeros and ones in the diagonal.

3 Answers   Infosys,


Categories