473,406 Members | 2,312 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,406 software developers and data experts.

program with std::cout<<

I write the following code

int x1 = 5;
std::cout<<x1--<<" "<<x1--<<" "<<x1;
and got the result
4 5 3
in VS2003
who can explain that, THX

Jul 22 '05 #1
4 1453
Sprite wrote:

I write the following code

int x1 = 5;
std::cout<<x1--<<" "<<x1--<<" "<<x1;
and got the result
4 5 3
in VS2003
who can explain that, THX


You encountered 'undefined behaviour'

You modify the same variable twice without an intermediate
sequence point.

Please search the groups archive at google. This point
has been beaten to death hundreds of times.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message news:41***************@gascad.at...
Sprite wrote:

I write the following code

int x1 = 5;
std::cout<<x1--<<" "<<x1--<<" "<<x1;
and got the result
4 5 3
in VS2003
who can explain that, THX


You encountered 'undefined behaviour'

You modify the same variable twice without an intermediate
sequence point.


What do you mean by "without an intermediate sequence point",
please?

Jul 22 '05 #3
Alan wrote:

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message news:41***************@gascad.at...
Sprite wrote:

I write the following code

int x1 = 5;
std::cout<<x1--<<" "<<x1--<<" "<<x1;
and got the result
4 5 3
in VS2003
who can explain that, THX


You encountered 'undefined behaviour'

You modify the same variable twice without an intermediate
sequence point.


What do you mean by "without an intermediate sequence point",
please?


As said: Please search the groups archive at google.

In short: C++ devides the execution of a program in sequences.
Each sequence is terminated with a sequence point (roughly:
every ';', every function call, ... denote a sequence point).
If you modify the same variable twice in a sequence, you have
undefined behaviour, as the compiler can do the actual assignment
anywhere within that sequence. There is only 1 restriction for the
compiler: it has to happen before the sequence point is reached. But
when exactly, is left to the compiler.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message news:41**************@gascad.at...
Alan wrote:

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message news:41***************@gascad.at...
Sprite wrote:
>
> I write the following code
>
> int x1 = 5;
> std::cout<<x1--<<" "<<x1--<<" "<<x1;
> and got the result
> 4 5 3
> in VS2003
> who can explain that, THX

You encountered 'undefined behaviour'

You modify the same variable twice without an intermediate
sequence point.


What do you mean by "without an intermediate sequence point",
please?


As said: Please search the groups archive at google.

In short: C++ devides the execution of a program in sequences.
Each sequence is terminated with a sequence point (roughly:
every ';', every function call, ... denote a sequence point).
If you modify the same variable twice in a sequence, you have
undefined behaviour, as the compiler can do the actual assignment
anywhere within that sequence. There is only 1 restriction for the
compiler: it has to happen before the sequence point is reached. But
when exactly, is left to the compiler.


Thanks Karl.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Ensoul Chee | last post by:
I used #include <iostream.h> int m; cout << "Hexadecimal == 0x" << hex << m << endl; to print value of m in hexadecimal mode. But I got the compile error like this couttest.cpp:20 `hex'...
12
by: Filipe Sousa | last post by:
Hi! Could someone explain to me why this operation is not what I was expecting? int main() { int x = 2; std::cout << x << " " << x++ << std::endl; return 0; }
7
by: imutate | last post by:
How do I implement << ala std::cout for vector template ? I already have the following: #include <vector> template < typename T > class Vec : public std::vector< T { public: Vec() { } Vec(...
4
by: ek | last post by:
I thought that the syntax for c++ in either winXP or linux was the same. In Ubuntu linux it works fine if I write: #include <string> using namespace std; int main() {
46
by: suresh | last post by:
Hi, For this code snippet, I get the following output, which I am unable to understand. (2^31 = 2147483648) cout<< -2147483648 << endl; cout << numeric_limits<int>::min() <<',' <<...
12
by: John Brawley | last post by:
Is this going to cause me a portability problem? Purpose: make it easy for me to write my 'help' text block in the .cpp file, just as I wish it to be output 'formatted-ly' to the user upon demand...
42
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.