004# puzzle of c

@dholey (1384)
India
January 22, 2007 11:18am CST
give the output of belove code , no cheating just paper pen work ,no online testing ... any ways explain the output too int main() { int x; for(x=1;x++
4 responses
• India
23 Jan 07
Yes... It’s pretty simple one… It will start by printing ‘2’ because of pre-increment in comparison…. Now, in each ‘printf’ statement, we are incrementing the value of ‘x’ two time… once in ‘execution statement’ and another in ‘comparison statement’ … so it will increase the value of x by ‘2’ every time. Now, the comparison is based on pre-increment … so x can go to 10 without quitting from the loop… so desired out put would be… 2 4 6 8 10
• India
23 Jan 07
Now, in each ‘printf’ statement please read it as:- Now, between each ‘printf’ statement
@dholey (1384)
• India
23 Jan 07
thanks for post and keep it up!!!! you have explained correctly
• India
22 Jan 07
I think that was a bit simple one. First on entering loop x is incremented by 1, so x would become 2 and it is printed. On the next loop xis incremented by 2 since it there is x++ in the test condition and another x++ in the second test condition. So x is incremented by 2 so it would become 4. So the final answer would be 2 4 6 8 10.
• India
22 Jan 07
Sorry there was a small error in what i typed above. This is what i meant "I think that was a bit simple one. First on entering loop x is incremented by 1 as there is a x++ in the test condition, so x would become 2 and it is printed. On the next loop x is incremented by 2 since it there is x++ in the test condition and another x++ in the increment part of the for loop. Thus 4 is printed. So the final answer would be 2 4 6 8 10."
@dholey (1384)
• India
23 Jan 07
every once output here is true and our comment is the first to solve so it is going to become the best response here
• India
12 May 07
2 4 6 8 10 .
• India
23 Jan 07
it will print the even numbers only. i.e. 2 4 6 8 10 this will be the output as you are incrementing the x twice in every statement of the loop.