|
Home > Archive > microsoft.public.cert.exam.mcsd > June 2003 > One code, 2 different results C# vs C++
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
One code, 2 different results C# vs C++
|
|
| Narayan Mohapatra 2003-06-27, 5:23 am |
| Hi,
Really its a good question.In C++ it is taking same memory
location thats why it is showing 84.
x += --x;
=> x = x + (--x)
=> x = 43(wrong) + 42 ( since x latest value = 42 and
its present in a same memory location thats why at that
same time all value will be 42.That is
=> x= 42 + 42 in stead of (43 + 42).
But in the case of C# ,i think this is not the case , when
you declare a datatype that is
int x , x treated as a object and two memory location
created ,hence result is coming 43 + 42 = 85
Cheers
Narayan
-----Original Message-----
>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? :P
>
>Regards,
>
>Caio Proiete
>MCSD (Visual C++)
>.
>
| |
| Kline Sphere 2003-06-27, 7:23 am |
| The order of precedence is formed differently in C# and Java, than is
C++
On Thu, 26 Jun 2003 19:10:23 -0700, "Caio Proiete"
<cproiete@nospam.proiete.com.br> wrote:
>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? :P
>
>Regards,
>
>Caio Proiete
>MCSD (Visual C++)
| |
| Fluker 2003-06-27, 8:23 am |
|
Intresting comment however I don't think the reasoning
is correct. I think it has to be difference in operator precendence.
"Narayan Mohapatra" <narayan@oliveinternet.com> wrote in message
news:097301c33c95$817ea460$a40
1280a@phx.gbl...
> Hi,
> Really its a good question.In C++ it is taking same memory
> location thats why it is showing 84.
>
> x += --x;
> => x = x + (--x)
> => x = 43(wrong) + 42 ( since x latest value = 42 and
> its present in a same memory location thats why at that
> same time all value will be 42.That is
> => x= 42 + 42 in stead of (43 + 42).
>
>
> But in the case of C# ,i think this is not the case , when
> you declare a datatype that is
>
> int x , x treated as a object and two memory location
> created ,hence result is coming 43 + 42 = 85
>
>
> Cheers
> Narayan
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> -----Original Message-----
> >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? :P
> >
> >Regards,
> >
> >Caio Proiete
> >MCSD (Visual C++)
> >.
> >
| |
|
| In article < 084701c33c51$4451b990$a101280a
@phx.gbl>,
cproiete@nospam.proiete.com.br 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? :P
>
> Regards,
>
> Caio Proiete
> MCSD (Visual C++)
>
+= takes priority over prefix decrement.
--
Thanks
DEK
| |
| Kline Sphere 2003-06-27, 9:23 am |
| > I think it has to be difference in operator precendence.
Correct
| |
| Caio Proiete 2003-06-27, 9:23 am |
| Well... maybe you're right...
So the expression:
x += --x
Would be
x = --(x + x)
nice... but weird :P
Thanks
Caio Proiete
MCSD (Visual C++)
>-----Original Message-----
>In article < 084701c33c51$4451b990$a101280a
@phx.gbl>,
>cproiete@nospam.proiete.com.br 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? :P
>>
>> Regards,
>>
>> Caio Proiete
>> MCSD (Visual C++)
>>
>
>+= takes priority over prefix decrement.
>--
>Thanks
> DEK
>.
>
| |
| Jamie 2003-06-27, 10:23 am |
| in C#, statements are evaluated from left to right, hence the first x
evaluates to 43, and the 2nd x (or rather the expression '--x')
evaluates to 42
i guess in C++, its different
from MSDN:
<snip>
The term "evaluated only once" means that in the evaluation of x op y,
the results of any constituent expressions of x are temporarily saved
and then reused when performing the assignment to x.
For example, in the assignment A()[B()] += C(), where A is a method
returning int[], and B and C are methods returning int, the methods
are invoked only once, in the order A, B, C.
</snip>
"Narayan Mohapatra" <narayan@oliveinternet.com> wrote in message news:< 097301c33c95$817ea460$a401280a
@phx.gbl>...
> Hi,
> Really its a good question.In C++ it is taking same memory
> location thats why it is showing 84.
>
> x += --x;
> => x = x + (--x)
> => x = 43(wrong) + 42 ( since x latest value = 42 and
> its present in a same memory location thats why at that
> same time all value will be 42.That is
> => x= 42 + 42 in stead of (43 + 42).
>
>
> But in the case of C# ,i think this is not the case , when
> you declare a datatype that is
>
> int x , x treated as a object and two memory location
> created ,hence result is coming 43 + 42 = 85
>
>
> Cheers
> Narayan
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> -----Original Message-----
> >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? :P
> >
> >Regards,
> >
> >Caio Proiete
> >MCSD (Visual C++)
> >.
> >
| |
| clyclopedic 2003-06-29, 8:23 am |
| It is clearly not the case that assignment operators have higher precedence
than prefix decrement in any of the languages in question.
"Caio Proiete" <cproiete@proiete.com.br> wrote in message
news:050d01c33cb2$669609a0$a00
1280a@phx.gbl...
> Well... maybe you're right...
>
> So the expression:
> x += --x
>
> Would be
> x = --(x + x)
>
> nice... but weird :P
>
> Thanks
>
> Caio Proiete
> MCSD (Visual C++)
>
>
> >-----Original Message-----
> >In article < 084701c33c51$4451b990$a101280a
@phx.gbl>,
> >cproiete@nospam.proiete.com.br 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? :P
> >>
> >> Regards,
> >>
> >> Caio Proiete
> >> MCSD (Visual C++)
> >>
> >
> >+= takes priority over prefix decrement.
> >--
> >Thanks
> > DEK
> >.
> >
| |
| Shailendra Sharma 2003-06-29, 9:23 am |
| Xref: intern1.nntp.aus1.giganews.com microsoft.public.cert.exam.mcsd:21964
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" <cproiete@proiete.com.br> wrote in message
> news:050d01c33cb2$669609a0$a00
1280a@phx.gbl...
>> Well... maybe you're right...
>>
>> So the expression:
>> x += --x
>>
>> Would be
>> x = --(x + x)
>>
>> nice... but weird :P
>>
>> Thanks
>>
>> Caio Proiete
>> MCSD (Visual C++)
>>
>>
>> >-----Original Message-----
>> >In article < 084701c33c51$4451b990$a101280a
@phx.gbl>,
>> >cproiete@nospam.proiete.com.br says...
>> >> Hi, I was just studying for my 70-315 test and had
one[c
olor=darkred]
>> >> 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[/color]
returns "85". [colo
r=darkred]
>> >> Just to confirm, I also compiled the same code with
>> Java
>> >> and returned 85 either.
>> >>
>> >> Well... can anyone explain this? :P
>> >>
>> >> Regards,
>> >>
>> >> Caio Proiete
>> >> MCSD (Visual C++)
>> >>
>> >
>> >+= takes priority over prefix decrement.
>> >--
>> >Thanks
>> > DEK
>> >.
>> >
>
>
>.
>[/color]
|
|
|
|
|