473,387 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

operator ++ in C++

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 ?
Apr 12 '07 #1
13 1726
sicarie
4,677 Expert Mod 4TB
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...
Apr 12 '07 #2
sicarie
4,677 Expert Mod 4TB
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...
Apr 12 '07 #3
sicarie
4,677 Expert Mod 4TB
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)
Apr 12 '07 #4
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 !)
Apr 12 '07 #5
sicarie
4,677 Expert Mod 4TB
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.
Apr 12 '07 #6
nmadct
83 Expert
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.
Apr 12 '07 #7
sicarie
4,677 Expert Mod 4TB
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!
Apr 12 '07 #8
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 ?
Apr 12 '07 #9
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 !
Apr 12 '07 #10
JosAH
11,448 Expert 8TB
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
Apr 12 '07 #11
nmadct
83 Expert
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.)
Apr 12 '07 #12
weaknessforcats
9,208 Expert Mod 8TB
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.
Apr 12 '07 #13
JosAH
11,448 Expert 8TB
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
Apr 12 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

7
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...
1
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...
5
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...
0
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...
3
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...
6
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 &)
3
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 '<'...
5
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...
8
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...
3
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
Oralloy
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.