How many of you enjoy solving puzzles of c/ c++ language....
By dholey
@dholey (1383)
India
12 responses
@satya_music (586)
• 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
@tmaheshwari (170)
• 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.
@anisurr2001 (13)
• 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
@nuno_canelas (1470)
• Portugal
15 Jan 07
Put more puzzles here please...it´s a great idea...
1 person likes this
@senthilp_1985 (30)
• 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
@senthilp_1985 (30)
• 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
@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?
@hora_fugit (5862)
• 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++ :)
@chhaganbhujbad (263)
• 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.