[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.c++

a question

c/c++ programming lover

11/7/2008 9:20:00 AM

Could anyone tell me why the result is that:
printf("%d",++5*++5);
the result is 49?
thanks
8 Answers

Juha Nieminen

11/7/2008 10:55:00 AM

0

c/c++ programming lover wrote:
> Could anyone tell me why the result is that:
> printf("%d",++5*++5);
> the result is 49?

error: lvalue required as increment operand

blargg.h4g

11/7/2008 11:03:00 AM

0

Juha Nieminen wrote:
> c/c++ programming lover wrote:
> > Could anyone tell me why the result is that:
> > printf("%d",++5*++5);
> > the result is 49?
>
> error: lvalue required as increment operand

Maybe 49 is the error code his compiler gives for such an error.

Victor Bazarov

11/7/2008 3:15:00 PM

0

blargg wrote:
> Juha Nieminen wrote:
>> c/c++ programming lover wrote:
>>> Could anyone tell me why the result is that:
>>> printf("%d",++5*++5);
>>> the result is 49?
>> error: lvalue required as increment operand
>
> Maybe 49 is the error code his compiler gives for such an error.

I think 'lover' just was asking about

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

Which has undefined behaviour last time I looked.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Salt_Peter

11/7/2008 9:20:00 PM

0

On Nov 7, 4:20 am, "c/c++ programming lover" <564462...@qq.com> wrote:
> Could anyone tell me why the result is that:
> printf("%d",++5*++5);
> the result is 49?
> thanks

It doesn't matter what the result is. Its undefined behavior.

Juha Nieminen

11/7/2008 10:50:00 PM

0

Salt_Peter wrote:
> On Nov 7, 4:20 am, "c/c++ programming lover" <564462...@qq.com> wrote:
>> Could anyone tell me why the result is that:
>> printf("%d",++5*++5);
>> the result is 49?
>> thanks
>
> It doesn't matter what the result is. Its undefined behavior.

Does the standard really specify a syntax error as "undefined behavior"?

Salt_Peter

11/7/2008 11:56:00 PM

0

On Nov 7, 5:49 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> Salt_Peter wrote:
> > On Nov 7, 4:20 am, "c/c++ programming lover" <564462...@qq.com> wrote:
> >> Could anyone tell me why the result is that:
> >> printf("%d",++5*++5);
> >> the result is 49?
> >> thanks
>
> > It doesn't matter what the result is. Its undefined behavior.
>
> Does the standard really specify a syntax error as "undefined behavior"?

Its a syntax error, yes, thats the obvious answer. Error or not, the
OP's intent was to ask what the following might have a guaranteed
result:

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

James Kanze

11/8/2008 9:20:00 AM

0

On Nov 7, 11:49 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> Salt_Peter wrote:
> > On Nov 7, 4:20 am, "c/c++ programming lover" <564462...@qq.com> wrote:
> >> Could anyone tell me why the result is that:
> >> printf("%d",++5*++5);
> >> the result is 49?
> >> thanks

> > It doesn't matter what the result is. Its undefined
> > behavior.

> Does the standard really specify a syntax error as "undefined
> behavior"?

It does, actually. The implementation is required to emit a
diagnostic, but what happens next is undefined behavior.

In practice, of course, from a QoI point of view, the
implementation will normally not generate an object file. The
rule is there to allow the implementation to use ill-formed
constructs as an extension; once the compiler has output a
diagnostic (the message "this is an extension", for example),
it's free to go on and compile the code, assigning any meaning
it wants to it.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

anon

11/10/2008 1:19:00 PM

0

Salt_Peter wrote:
> On Nov 7, 5:49 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
>> Salt_Peter wrote:
>>> On Nov 7, 4:20 am, "c/c++ programming lover" <564462...@qq.com> wrote:
>>>> Could anyone tell me why the result is that:
>>>> printf("%d",++5*++5);
>>>> the result is 49?
>>>> thanks
>>> It doesn't matter what the result is. Its undefined behavior.
>> Does the standard really specify a syntax error as "undefined behavior"?
>
> Its a syntax error, yes, thats the obvious answer. Error or not, the
> OP's intent was to ask what the following might have a guaranteed
> result:
>
> int n(5);
> printf("%d",++n*++n);

Intent or not, what he wrote produces a syntax error.