[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

a new question

c/c++ programming lover

11/13/2008 3:24:00 AM

I'm sorry I made a mistake last time,Now the correct question is :
#include<stdio.h>
int main()
{
int a=5;
printf("%d",++a*++a);
return 0;
}
the result is 49.Could anyone tell me why?
thank you.
9 Answers

Ian Collins

11/13/2008 3:32:00 AM

0

c/c++ programming lover wrote:
> I'm sorry I made a mistake last time,Now the correct question is :
> #include<stdio.h>
> int main()
> {
> int a=5;
> printf("%d",++a*++a);
> return 0;
> }
> the result is 49.Could anyone tell me why?

They already have.

--
Ian Collins

Michael

11/13/2008 4:56:00 AM

0

c/c++ programming lover wrote:
> I'm sorry I made a mistake last time,Now the correct question is :
> #include<stdio.h>
> int main()
> {
> int a=5;
> printf("%d",++a*++a);
> return 0;
> }
> the result is 49.Could anyone tell me why?
> thank you.
++a*++a is undefined behaviour, the compiler can produce whatever it
likes. For example, it can yield 36,42,49,999,-1,segfault,or even launch
a rocket (if there is suitable hardware connected to it).

Andrey Tarasevich

11/13/2008 5:33:00 AM

0

c/c++ programming lover wrote:
> I'm sorry I made a mistake last time,Now the correct question is :
> #include<stdio.h>
> int main()
> {
> int a=5;
> printf("%d",++a*++a);
> return 0;
> }
> the result is 49.Could anyone tell me why?

Your question has already been answered in your original thread.
Expression '++a * ++a' has no meaning in C++. It produces undefined
behavior, because it modifies the same variable twice. The result is
unpredictable, meaning that there's no answer to your "why".

--
Best regards,
Andrey Tarasevich

Salt_Peter

11/13/2008 5:41:00 AM

0

On Nov 12, 10:23 pm, "c/c++ programming lover" <564462...@qq.com>
wrote:
> I'm sorry I made a mistake last time,Now the correct question is :
> #include<stdio.h>
> int main()
> {
>     int a=5;
>     printf("%d",++a*++a);
>     return 0;}
>
> the result is 49.Could anyone tell me why?
> thank you.

its undefined behavior, as was explained to you last time you asked.
That means that what result you might get is not guaranteed by the
language.
The fact that you got 49 is irrelevant.

You have to use sequence points to get a defined result:

int a=5;
++a;
++a;
printf("%d",a * a);

Rolf Magnus

11/13/2008 6:21:00 PM

0

Michael wrote:

> c/c++ programming lover wrote:
>> I'm sorry I made a mistake last time,Now the correct question is :
>> #include<stdio.h>
>> int main()
>> {
>> int a=5;
>> printf("%d",++a*++a);
>> return 0;
>> }
>> the result is 49.Could anyone tell me why?
>> thank you.
> ++a*++a is undefined behaviour, the compiler can produce whatever it
> likes. For example, it can yield 36,42,49,999,-1,segfault,or even launch
> a rocket (if there is suitable hardware connected to it).

Nope. The C++ standard doesn't require a suitable hardware. Without it,
the nuclear strike may be unlikely, but not forbidden by the standard.


Rolf Magnus

11/13/2008 6:21:00 PM

0

c/c++ programming lover wrote:

> I'm sorry I made a mistake last time,Now the correct question is :
> #include<stdio.h>
> int main()
> {
> int a=5;
> printf("%d",++a*++a);
> return 0;
> }
> the result is 49.Could anyone tell me why?

The answer is: Why not?


Juha Nieminen

11/13/2008 7:32:00 PM

0

Michael wrote:
> ++a*++a is undefined behaviour, the compiler can produce whatever it
> likes. For example, it can yield 36,42,49,999,-1,segfault,or even launch
> a rocket (if there is suitable hardware connected to it).

Isn't that contradictory with the standard definition of the prefix
operator ++, as using it is not undefined behavior?

Does the standard really explicitly state that it's undefined
behavior, rather than saying something along the lines that the
(numerical) result is undefined? (In other words, the expression itself
is completely valid and will produce an integer value as result. It's
just not guaranteed what that value will be.)

Andrey Tarasevich

11/13/2008 7:45:00 PM

0

Juha Nieminen wrote:
> Does the standard really explicitly state that it's undefined
> behavior,

Yes. 5/4.

--
Best regards,
Andrey Tarasevich

Pete Becker

11/13/2008 8:15:00 PM

0

On 2008-11-13 14:31:50 -0500, Juha Nieminen <nospam@thanks.invalid> said:

> Michael wrote:
>> ++a*++a is undefined behaviour, the compiler can produce whatever it
>> likes. For example, it can yield 36,42,49,999,-1,segfault,or even launch
>> a rocket (if there is suitable hardware connected to it).
>
> Isn't that contradictory with the standard definition of the prefix
> operator ++, as using it is not undefined behavior?

The problem isn't with the prefix operator++. It's with combining two of them.

>
> Does the standard really explicitly state that it's undefined
> behavior, rather than saying something along the lines that the
> (numerical) result is undefined?

Yes.

> (In other words, the expression itself
> is completely valid and will produce an integer value as result. It's
> just not guaranteed what that value will be.)

The behavior is undefined.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)