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 is output of the following statetment?Printf(“%x”, -1<<4); ?

5 Answers  


At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with these names could be kept in that shelf as in this example: bottom of shelf ---> [AAAJKRDFDEWAAYFYYKK]-----Top of shelf. All these packets are to be loaded on cars. The cars are lined in order, so that the packet could be loaded on them. The cars are also named [A, B, C, D, E,………….]. Each Car will load the packet with the same alphabet. So, for example, car ‘A’ will load all the packets with name ‘A’. Each particular car will come at the loading point only once. The cars will come at the loading point in alphabetical order. So, car ‘B’ will come and take all the packets with name ‘B’ from the shelf, then car ‘C’ will come. No matter how deep in the shelf any packet ‘B’ is, all of the ‘B’ packets will be displaced before the ‘C’ car arrives. For that purpose, some additional shelves are provided. The packets which are after the packet B, are kept in those shelves. Any one of these shelves contains only packets, having the same name. For example, if any particular shelf is used and if a packet with name X is in it, then only the packets having names X will be kept in it. That shelf will look like [XXXXXXX]. If any shelf is used once, then it could be used again only if it is vacant. Packets from the initial shelf could be unloaded from top only. Write a program that finds the minimum total number of shelves, including the initial one required for this loading process.

0 Answers   Infosys,


1)what is the error in the following stmt where str is a char array and the stmt is supposed to traverse through the whole character string str? for(i=0;str[i];i++) a)There is no error. b)There shud be no ; after the stmt. c)The cond shud be str[i]!='\0' d)The cond shud be str[i]!=NULL e)i shud be initialized to 1

4 Answers  


What is a wrapper function in c?

0 Answers  


How can you print HELLO WORLD without using "semicolon"?

7 Answers   HCL, Infosys,






What is main () in c?

0 Answers  


What are the complete rules for header file searching?

0 Answers  


Can we replace the struct function in tree syntax with a union?

0 Answers   Huawei,


Why does not c have an exponentiation operator?

0 Answers  


the number of measuring units from a arbitrary starting point in a record area or control block to some other point a) branching b) recording pointer c) none d) offset

0 Answers  


which of the following statement is wrong a) mes=123.56; b) con='T'*'A'; c) this='T'*20; d) 3+a=b;

0 Answers  


What is the use of a semicolon (;) at the end of every program statement?

1 Answers  


Categories