|
Home > Archive > microsoft.public.cert.exam.mcsd > June 2003 > Re: 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 |
Re: One code, 2 different results C# vs C++
|
|
| PJ Lee 2003-06-27, 5:23 am |
| Hello,
> int myFunc()
> {
> int x;
> x = 42; //x = 42
> x++; //x now equals 43
> x += --x; //since the '--' is on the left side of the second x, this
will be evaluated frist, hence making value on the right of the '+=' equals
to 42, 43 + 42 = 85.
> return x;
> }
hth
Pete
"Caio Proiete" <cproiete@nospam.proiete.com.br> wrote in message
news:084701c33c51$4451b990$a10
1280a@phx.gbl...
> 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++)
| |
| Caio Proiete 2003-06-27, 9:23 am |
| PJ Lee, I understand your point but doesn't x points to
one UNIQUE place?
If the -- operator is evaluated first, then when the += is
going to be evaluated, the value of X is already
decremented.
So x += --x ---> x = 42 + 42 = 84
Regards,
Caio Proiete
MCSD (Visual C++)
>-----Original Message-----
>Hello,
>
>> int myFunc()
>> {
>> int x;
>> x = 42; //x = 42
>> x++; //x now equals 43
>> x += --x; //since the '--' is on the left side of
the second x, this
>will be evaluated frist, hence making value on the right
of the '+=' equals
>to 42, 43 + 42 = 85.
>> return x;
>> }
>
>hth
>
>Pete
>
>"Caio Proiete" <cproiete@nospam.proiete.com.br> wrote in
message
> news:084701c33c51$4451b990$a10
1280a@phx.gbl...
>> 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++)
>
>
>.
>
|
|
|
|
|