I think that's not undefined behavior the compiler translates as follows:

the first operation:
the compiler "sees" 2 "++" prefixes and so it translates them making x = 11 so 
the answer is 33
the second operation is exactly like the first one and since no space between 
the two outputs then 3333 is written to the screen.

the third operation:
the comiler sees one ++ postfix so it translates it but after the "cout" so the 
output is 333327

the fourth operation is exactly like the third but for one difference; the 
"cout" is translated after the translation of the computations so the 
incrementation in x is executed from 27 to 28. 

so the final output is 33332728.

if any one find that wrong please say :)



so any one can show how the compiler translate that code into assembly?
Nico Heinze <[EMAIL PROTECTED]> wrote:                                  --- In 
[email protected], David <[EMAIL PROTECTED]> wrote:
 > 
 > dokhtar_e_sharghi1987z a �crit :
 > > hi all,
 > >
 > > #include<iostream.h>
 > > main()
 > > { int x ;
 > >   x=9 ; x=++x + ++x + x; cout<<x ;
 > >   x=9 ; cout<<++x + ++x + x ;
 > >   x=9 ; cout<<x + x++ +x ;
 > >   x=9 ; x=x + x++ +x; cout<<x ;
 > > return 0;
 > > }
 > >  
 > > why the out put is: 33332728
 > 
 > by luck
 > 
 > btw, iostream.h is deprecated, main() must be at least
 > int main(), and return 0; is implicit in C++ at the main
 > of the main
 > probably someone willl advise you to change your compiler.
 
 Good short reply.
 Here's the longer version: as you constantly change x several times
 between sequence points (roughly spoken, between two semicolons), you
 invoke undefined behaviour. And the term "undefined behaviour" means
 exactly what it says: you're doing something which is silly to do.
 
 You don't know what undefined behaviour is? Then go to Wikipedia,
 enter the search word "Nasal Demon", and look under the second link
 ("undefined behaviour").
 
 Regards,
 Nico
 
 
     
                       


--------
Mohammad Nabil Mohammad.
3rd Year Computer Engineering Department,
Cairo University.
ÓÃá Çáããßä ÇáãÓÊÍíá : Ãíä ÊÞíã ¿ ÝÃÌÇÈå Ýí ÃÍáÇã ÇáÚÇÌÒ

       
---------------------------------
Ahhh...imagining that irresistible "new car" smell?
 Check outnew cars at Yahoo! Autos.

[Non-text portions of this message have been removed]

Reply via email to