How many of you enjoy solving puzzles of c/ c++ language....

c puzzle - ansi c, c by karnighan and ritchie
@dholey (1383)
India
November 27, 2006 6:21am CST
here is a c puzzle: can u tell me the output... void main() { int x=10; printf("\n\t%d%d%d%d",--x,--x,x++,++x); }
3 people like this
12 responses
• India
27 Dec 06
this puzzle is based on ASSOCIATIVITY of ++/-- operators i can solve it ... let me try ... the output will be .. 10 11 11 11 AM I RIGHT .... i liked this discussion, i will add more puzzle here , and i will love to see more puzzles from you too
1 person likes this
@dholey (1383)
• India
12 Jan 07
i will add more puzzles soon
@dholey (1383)
• India
12 Jan 07
you are absolutely right
• India
15 Jan 07
This same question was asked to me during my Infosys technical interview.I answered it.But I lost the next half of the interview ,the HR-lady was too sexy.
• India
13 Jan 07
the result of this depends and vary compiler to compiler. actually x++ and ++x are treated diffrently inside. x++ require a temporary variable which can change the actuall result. furthermore, comiler must resolve the value of x before calling printf routine.
1 person likes this
@dholey (1383)
• India
13 Jan 07
as far as my knowledge is concern , every compiler treats ++ in same manner and there associativity is same.
• India
13 Feb 07
Yah ++ and -- are treated the same way by all c/c++ compiler. IT would give output: 10 11 11 11
@paki143 (793)
• Pakistan
14 Jan 07
yes i am interested in that types of puzzles i am doing graduation in computer science and i learn c language last month so i am enjoying in making c programs and puzzles output is 9 8 8 10 sir am i right or wrong ???
@dholey (1383)
• India
20 Jan 07
the associativity of ++ and -- is right to left , that means they starts there evaluation from right to left , then comes the point called pre increment/ecrement, and post increment/decrement in pre incre/decre the value of variable is incresed/decreased first then the work is performed but in post incre/decre first of all the work is performed then the variable is increased or decreased , now check it you will get it with this knowledge...
@dholey (1383)
• India
14 Jan 07
if i am right your real name is zubair... am i right? the correct output is selected as best response, actually it(++/--) has associativity from right to left so first of all the right most operator is handled then last but one and so on ... hope you have got it .....
1 person likes this
@paki143 (793)
• Pakistan
15 Jan 07
sorry sir i not understand it pls give me the correct output and my real name is uzair
@prokrypt (67)
• United States
14 Jan 07
Oh this is weird... I compiled your bit of code with GCC, ran it, and got "10101110" as output...
1 person likes this
@dholey (1383)
• India
14 Jan 07
thanks for updates , i will check it by my elf ... thanks a lot
1 person likes this
• Portugal
15 Jan 07
Put more puzzles here please...it´s a great idea...
1 person likes this
@dholey (1383)
• India
20 Jan 07
you can find few new puzzles in my dicussions
• India
28 Jan 07
Hello dholey, output will be: 98810 Reason: 1. 1st nextline and tab sequence 2. x decrements twice and gets printed as 98 3. prints 8 and x post increments as x=9 4. Now preincrements as x=10 and 10 gets printed
• India
28 Jan 07
But as you said associativity will not be concerned with all compilers. If we take that this is how output will be. 1. Associativity from right. preincrement x=11 hence prints 11. 2. post increment. print 11 again x increments to x=12. 3. Double pre increment printing values as 11 and 10 so output: 11121110
• India
28 Jan 07
sorry but prints as 10111211
• India
28 Jan 07
10111111
@tamanash (950)
• India
15 Feb 07
its really a nice discussion.this problem basically test your knowledge about postfix and prefix oprators.in my opinion the output is 10 9 11 11.maybe i am wrong.this type of puzzeles are common in placement papers.nice to solve it in mylot and earn few cents by doing so.
@kingadnan (1538)
• Pakistan
23 Jan 07
dholey friend you should also post JAVA code then we will guess. i am not a student of c language..please teach me C language ... send me private message and teach me C language. please
@dholey (1383)
• India
24 Jan 07
if you know java , ou can answer c questions too, their behavior is same , only difference is ... java is pure oops , c is not , c++ is oops but not strictly enough like java, but variable creation, condition checking, looping,array, functions all these are same in these languages, so keep posting on puzzles sections of my discussions
1 person likes this
@kingadnan (1538)
• Pakistan
17 Feb 07
yes but there printf line is complicated for me, but i will try my best, i am student yet so i have to strong my coding base, i will also start my java puzzle discussion, then we will challenge each other ok dholey uncle please teach me c/c++ and help me i want to strong my base in loopings, my looping is too weak.
@hora_fugit (5862)
• India
12 Feb 07
I see all the wrong answers here are explained and given in steps but right ones are just put here. If it starts from right, then there must be following steps: OutPut 1. ++x = 11 (as x is incremented first) 2. x++ = 11 (x is now 12) 3. --x = 11 (as x is decremented first) 4. --x = 10 (as above) Why not?
• India
12 Feb 07
Oh sorry, I had written that to complain, but now I see I have got the point. My answer too is 10111111. But you should explain stepwise! In my case I must add that though we start from right, output will be from left i.e. steps 4-3-2-1 Thanks for the puzzle. I was really missing my c++ :)
• United States
15 Feb 07
Well Once I start learning C++ which is going ot be in the next semester, it would be fun solving puzzles and coding.. lets see how that will work out
@hi_gourab (242)
• India
13 Feb 07
10111111 In my view this should be the output as the stack concept reveals.
• India
12 May 07
10 11 11 11