#include <iostream.h>
int main()
{
int i = 0;
cout << "i++ = " << i++ << endl
<< "++i = " << ++i << endl
<< "i++ = " << i++ << endl
<< "Now i = " << i << endl;
return 0;
}
The output is:
i++ = 0
++i = 3
i++ = 2
Now i = 3
Pretty interesting! isn't it?
Now here is the output from the same program compiled by regular GNU compiler
g++:
i++ = 0
++i = 2
i++ = 2
Now i = 3
Is it a bug in the C Set++ or is it an ambigurity of the language?
Hsiao-Min Wu
hs...@teleport.com
You cannot assume that in a single cout statement the commands are executed
from left to right (or top to bottom). When you put single cout statements,
you get the result you anticipated.
--
Henning Lebius leb...@utkux.utk.edu
'Never trust a idiot with a watermelon and a hammer' - Gallagher
At 28-06-95 mdg-h squeezed among other things this out of his/her keyboard:
m> Its a bug, Send it in to Cset2 group.
No, it's not a bug. The expression is undefined.
Compare to 'The C++ programming language' by Bjarne Stroustrup,
referenz-handbook R.5 Expressions.
The sequence in which expressions are evaluated depends on priority-rules and
the grouping of the expression. The math-rules of assoziations and
Kommutativit„t (don't know the english term) are only true for those operators
which are really assoziativ and Kommutativ. If not defined elsewhere, the
sequence of evaluation of an expression is undefined.
Examples:
i = v[i++]; // value of i is undefined
i=7, i++, i++; // i has the value 9.
So it's legally for a compiler i.e. to first evaluate all mathematical
expressions in an cout <<... expression before evaluating all the other
expressions.
Until the next mail...,
ste...@acme2.ruhr.de
2:2444/1404.0@fidonet
Hi mdg-h,
Du meintest am 28.06.95 um 06:22 im Brett /COMP/OS/OS2/PROGRAMMER/MISC:
mh> >#include <iostream.h>
mh> >int main()
mh> >{
mh> > int i = 0;
mh> > cout << "i++ = " << i++ << endl
mh> > << "++i = " << ++i << endl
mh> > << "i++ = " << i++ << endl
mh> > << "Now i = " << i << endl;
mh> > return 0;
mh> >}
mh> >
mh> >The output is:
mh> >
mh> >i++ = 0
mh> >++i = 3
mh> >i++ = 2
mh> >Now i = 3
mh> >
mh> >Pretty interesting! isn't it?
mh> Its a bug, Send it in to Cset2 group.
sorry, but IMO this is in full correspondence to the language
definition.
The evaluation order is undefined as all expressions are within one
statement. Borland (according to poor optimization?) has AFAIK the
only compiler that will give the output which is mostly expected by
novices (0,1,2,3).
Take care of this when porting a BC Source to annother Compiler...
(I had to learn this the hard way :-( ).
cu/2
-> echo me, Arie <-- If Windows 9? is the answer,-
who ever did ask this stupid question?
## CrossPoint v3.0 R ##