Please help me explain this .
I' m using MS Visual C++6.0 . First i take the initialization :int i=5;
>if i use printf("%d %d %d",i++,i++,i++) ;the output is 5 5 5
>if i use cout<<i++<<i++<<i++; the output is 7 6 5 !
It's so confusing ! Due to the residual -value law of the post increment operator i think the output must be 5 6 7 ! Why 7 6 5 - a reverse order ?
And more , printf and cout are different , but I wonder why ?
13 1726
Please help me explain this .
I' m using MS Visual C++6.0 . First i take the initialization :int i=5;
>if i use printf("%d %d %d",i++,i++,i++) ;the output is 5 5 5
>if i use cout<<i++<<i++<<i++; the output is 7 6 5 !
It's so confusing ! Due to the residual -value law of the post increment operator i think the output must be 5 6 7 ! Why 7 6 5 - a reverse order ?
And more , printf and cout are different , but I wonder why ?
I'm not entirely sure on this, but I'd guess that it's because printf has parentheses - it's interpreted as a single statement, the arguments are not taken all at once - they're dealt with as they are given to cout, so the first one is done, incremented, and then the second one is taken, and after printed, is incremented, etc...
Actually, I just ran that in Cygwin, and it gave me (with i initialized as 1):
1 2 3
4 5 6
I think that might be something with the VC++ compiler...
Actually, I just ran that in Cygwin, and it gave me (with i initialized as 1):
1 2 3
4 5 6
I think that might be something with the VC++ compiler...
Yep, just ran it on Ubuntu and got
3 2 1 (I did cout first, though)
4 5 6 (and this was the printf line)
Thank you for answering .
But i'm still confused .Because when i write printf("%d %d %d",++i,++i,++i) the result is 8 7 6 ( not 6 7 8 !)
Thank you for answering .
But i'm still confused .Because when i write printf("%d %d %d",++i,++i,++i) the result is 8 7 6 ( not 6 7 8 !)
I'm pretty sure it's compiler and platform dependent - it depends on how the people who wrote your OS, and then the people who wrote your compiler for your OS, decided to do it. It's really bizzare to me that they print out backwards.
Surprising but easily explained, it would seem that the VC grammar defines argument lists in a right-recursive manner so that the rightmost term is the innermost level of the syntax tree and is evaluated first.
Surprising but easily explained, it would seem that the VC grammar defines argument lists in a right-recursive manner so that the rightmost term is the innermost level of the syntax tree and is evaluated first.
Seems GCC is that way as well, thanks nmadct!
hi
Always the statement is executed from right to left order, and in printf() you used i++ that says first use then increment, and since the parameter are instantiated at same instance hence all three values of i were taken as 5 if you use another statement there you will find the incremented value for i.
as for cout it passes as individual recursive call from right to left hence first the rightmost value is passed i.e. i++ this will return 5 but the actual value will b incremented. then second right most value i++ will use the incremented value i.e 6 and so the third right most value will take it 7 hence the output will be - 7 6 5.
hope this explained you.
cheers
Please help me explain this .
I' m using MS Visual C++6.0 . First i take the initialization :int i=5;
>if i use printf("%d %d %d",i++,i++,i++) ;the output is 5 5 5
>if i use cout<<i++<<i++<<i++; the output is 7 6 5 !
It's so confusing ! Due to the residual -value law of the post increment operator i think the output must be 5 6 7 ! Why 7 6 5 - a reverse order ?
And more , printf and cout are different , but I wonder why ?
But the compiler only reads from the right with integers ?
for ex , i write int i=5; cout<<"U "<<++i<<"have "<<++i;
it displays "U 7 have 6" .So the compiler works in such a funny way :go through ,write all strings and steps backward with int ! I think you are right , but it's funny !
When a modifiable lhv (Left Hand Value) is modified more than once before a
sequence point is reached the behaviour will be undedined. An expression such
as cout << i++ << i++ << i++ does just that: modifying a lhv more than once
before a sequence point is reached and thus induces undefined behaviour.
No more can be said about it.
kind regards,
Jos
The behavior is undefined by the standard, as Jos said, but obviously a particular compiler implementation defines a behavior, whatever that may be. It might be fun to poke around and figure out what that behavior is. You should never write any program that depends on that behavior though. Even if you are targeting only one particular compiler and you know how it behaves regarding this issue, there's no guarantee that it won't change tomorrow. (In the case of GCC we can know exactly what it does if we care to, because we have the source code.)
The correct sequence to display is 7 6 5.
This code:
cout<<i++<<i++<<i++;
is really:
operator<<((operator<<(cout, i++), i++), i++);
where i is 5 6 7
That is, a nested call where function calls are used as function arguments.
The outer call is made first, Hence 7.
To make next inner call you need to call the left argument. i will be 6
To make the inner call you need to call the inner inner left argument. i will be 5.
Using printf(), you get the same results.
The correct sequence to display is 7 6 5.
Nope, no matter what clever sophistic reasoning you apply, the expression
results in undefined behaviour for reasons I wrote in a previous reply. Maybe
sad but true. btw, not even the order of parameter evaluation is specified so
that is another problem w.r.t. undefined behaviour.
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Paul Davis |
last post by:
I'd like to overload 'comma' to define a concatenation operator for
integer-like classes. I've got some first ideas, but I'd appreciate a
sanity check. The concatenation operator needs to so...
|
by: joesoap |
last post by:
Hi can anybody please tell me what is wrong with my ostream operator???
this is the output i get using the 3 attached files.
this is the output after i run assignment2
-joesoap
#include...
|
by: Jason |
last post by:
Hello. I am trying to learn how operator overloading works so I wrote
a simple class to help me practice. I understand the basic opertoar
overload like + - / *, but when I try to overload more...
|
by: Martin Magnusson |
last post by:
I have defined a number of custom stream buffers with corresponding in
and out streams for IO operations in my program, such as IO::output,
IO::warning and IO::debug. Now, the debug stream should...
|
by: Sensei |
last post by:
Hi.
I have a problem with a C++ code I can't resolve, or better, I can't see
what the problem should be!
Here's an excerpt of the incriminated code:
=== bspalgo.cpp
// THAT'S THE BAD...
|
by: YUY0x7 |
last post by:
Hi, I am having a bit of trouble with a specialization of operator<<.
Here goes:
class MyStream
{
};
template <typename T>
MyStream& operator<<(MyStream& lhs, T const &)
|
by: gugdias |
last post by:
I'm coding a simple matrix class, which is resulting in the following
error when compiling with g++ 3.4.2 (mingw-special):
* declaration of `operator/' as non-function
* expected `;' before '<'...
|
by: raylopez99 |
last post by:
I need an example of a managed overloaded assignment operator for a
reference class, so I can equate two classes A1 and A2, say called
ARefClass, in this manner: A1=A2;. For some strange reason...
|
by: valerij |
last post by:
Yes, hi
How to write "operator +" and "operator =" functions in a class with
a defined constructor? The following code demonstrates that I don't
really understand how to do it... I think it has...
|
by: y-man |
last post by:
Hi,
I am trying to get an overloaded operator to work inside the class it
works on. The situation is something like this:
main.cc:
#include "object.hh"
#include "somefile.hh"
object obj,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |