void main()
{
int var1 = 10;
cout<<var1<<" "<<var1++<<" "<<++var1<<" "<<var1++;
}
And why? Thanks for your inputs.
Printable View
void main()
{
int var1 = 10;
cout<<var1<<" "<<var1++<<" "<<++var1<<" "<<var1++;
}
And why? Thanks for your inputs.
Undefined due to the errors.
Maybe I should expand on that:Quote:
Originally Posted by dullboy
- it's int main(), not void main()
- you cannot modify the stored value of a variable more than once between sequence points. This is what the "cout" line is doing. The behaviour is undefined, therefore the output of the program cannot be predicted.
The output is:
'cout' : undeclared identifier
because you forgot to include <iostream.h>
Seriously, though, it should be:
13, 12, 12, 10
Starting at the right, the variable value is printed, then incremented. So, "10" goes to cout (eventually). Then it is incremented to 11.
The increment that occurs to the left of this happens before the value is sent to cout, so it is incremented to 12, then printed.
Similarly with the last (err, first) two.
Thanks for your reply. But if you run this program, output would be "13 12 12 10". I don't understand the output.
Quote:
Originally Posted by Graham
Scratch the .h and we agree.Quote:
Originally Posted by Bond
There is no guarantee that the right expression will be evaluated first, so the results are, as Graham correctly pointed out, undefined.Quote:
Originally Posted by Bond
But my question is that why it is starting at the right instead of left? For example, int x = 1; int y = 2; cout<<x<<y; Then the output is 1 2 instead of 2 1.
Quote:
Originally Posted by Bond
Don't try to understand the output. If you ran this on another compiler, in all likelihood the output is different. That is what is meant by "undefined behavior".Quote:
Originally Posted by dullboy
Regards,
Paul McKenzie
Even you are right, there is always logics behind the output, which I try to understand.
Quote:
Originally Posted by Paul McKenzie
Why not concentrate on learning how not to write code like this, and concentrate on proper C++ coding techniques?Quote:
Originally Posted by dullboy
There is no "logic" behind the output. Once you introduce undefined behavior, then anything under the hood could be happening. The answer can be 1,489,423,324 for all it's worth. Again, the behavior is undefined.
Regards,
Paul McKenzie
operator<< is a function, the order of evaluation for function arguments are undefined: http://www.research.att.com/~bs/bs_f...aluation-order
If you want to know why your compiler is evaluating function arguments from right to left or vice-versa, ask your compiler vendor.
Look, it's quite simple: you are not allowed to modify the stored value of a variable more than once between sequence points. This program attempts to modify it three times between sequence points. Order of evaluation of arguments is totally irrelevant - it's an illegal statement.
And just in case it needs hammering home, here's the relevant paragraph from the standard (Section 5, para 4):
Quote:
Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified. Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; otherwise the behavior is undefined.
Yes, operator<<() is a function, but something likeQuote:
Originally Posted by cma
is not one function call. First, the compiler looks atCode:cout << a << b;
cout << a, and calls the corresponding operator<<() function (which takes *one*argument, so there is no question about the order of evaluation of function arguments). The function then returns the ostream value that invoked it (in this case, cout) so now the expression looks like this:
This once again evaluates to the right operator<<() call.Code:cout << b;
And yes, the operator<<() call that processes "a" is called first, and the operator<<() call that processes "b" is called second and this is perfectly well defined and that's why "a" gets inserted into the output stream before "b" does.
The only reason
is undefined is because you are trying to modify the *same* variable three times. If it were 3 different variables, as such.Code:cout<<var1<<" "<<var1++<<" "<<++var1<<" "<<var1++;
then everything, including the output and the order in which the functions are called, is well defined.Code:int var1, var2, var3;
... // initialize them to something
cout<<var1<<" "<<var1++<<" "<<++var2<<" "<<var3++;
Note that
is OK because even though you're outputting the same variable twice, you're only actually modifying it once.Code:cout << var1 << " " << var1++;
Nitpick: it takes two arguments - don't forget the ostream&. Otherwise, you're quite right.Quote:
Originally Posted by HighCommander4