![]() |
|
|
|||||||
![]() |
MCSD - Re: One code, 2 different results C# vs C++ |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
It is clearly not the case that assignment operators have higher precedence
than prefix decrement in any of the languages in question. "Caio Proiete" <> wrote in message news:050d01c33cb2$669609a0$... > Well... maybe you're right... > > So the expression: > x += --x > > Would be > x = --(x + x) > > nice... but weird > > Thanks > > Caio Proiete > MCSD (Visual C++) > > > >-----Original Message----- > >In article <084701c33c51$4451b990$>, > > says... > >> Hi, I was just studying for my 70-315 test and had one > >> surprise. Look at this code: > >> > >> int myFunc() > >> { > >> int x; > >> x = 42; > >> x++; > >> x += --x; > >> return x; > >> } > >> > >> What is the value returned from myFunc()? > >> As a C++ programmer I said: "84 of course". > >> > >> But when I compiled with C#, the function returns "85". > >> Just to confirm, I also compiled the same code with > Java > >> and returned 85 either. > >> > >> Well... can anyone explain this? > >> > >> Regards, > >> > >> Caio Proiete > >> MCSD (Visual C++) > >> > > > >+= takes priority over prefix decrement. > >-- > >Thanks > > DEK > >. > > clyclopedic |
|
|
|
|
#2 |
|
Posts: n/a
|
Absolutely Correct....
>-----Original Message----- >It is clearly not the case that assignment operators have higher precedence >than prefix decrement in any of the languages in question. > >"Caio Proiete" <> wrote in message >news:050d01c33cb2$669609a0$... >> Well... maybe you're right... >> >> So the expression: >> x += --x >> >> Would be >> x = --(x + x) >> >> nice... but weird >> >> Thanks >> >> Caio Proiete >> MCSD (Visual C++) >> >> >> >-----Original Message----- >> >In article <084701c33c51$4451b990$>, >> > says... >> >> Hi, I was just studying for my 70-315 test and had one >> >> surprise. Look at this code: >> >> >> >> int myFunc() >> >> { >> >> int x; >> >> x = 42; >> >> x++; >> >> x += --x; >> >> return x; >> >> } >> >> >> >> What is the value returned from myFunc()? >> >> As a C++ programmer I said: "84 of course". >> >> >> >> But when I compiled with C#, the function returns "85". >> >> Just to confirm, I also compiled the same code with >> Java >> >> and returned 85 either. >> >> >> >> Well... can anyone explain this? >> >> >> >> Regards, >> >> >> >> Caio Proiete >> >> MCSD (Visual C++) >> >> >> > >> >+= takes priority over prefix decrement. >> >-- >> >Thanks >> > DEK >> >. >> > > > >. > |
|