473,320 Members | 1,902 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,320 software developers and data experts.

Sequence points.

Hello,
I was solving a C++ quiz that has the following question:
What is the outcome of running the following code:

int c=0;
cout<<c++<<c;

The answer says undefined. But in this case, isn't this statement
translated into:

cout.operator<<(c++).operator<<(c);

There should be a sequnce point before calling a funcion after
evaluation of the arguments. So shouldn't the incremented value of c be
stored before the call to first <<. If so it should be available for
the second <<, and the result would be 01.

Please help me with this confusion.

May 26 '06 #1
7 1662
kw******@gmail.com wrote:
Hello,
I was solving a C++ quiz that has the following question:
What is the outcome of running the following code:

int c=0;
cout<<c++<<c;

The answer says undefined. But in this case, isn't this statement
translated into:

cout.operator<<(c++).operator<<(c);

There should be a sequnce point before calling a funcion after
evaluation of the arguments. So shouldn't the incremented value of c be
stored before the call to first <<. If so it should be available for
the second <<, and the result would be 01.

Please help me with this confusion.


Compilers have right to delay the increment operation on c until the end
of the statement.

Ben
May 26 '06 #2
kwrza...@gmail.com wrote:
Hello,
I was solving a C++ quiz that has the following question:
What is the outcome of running the following code:

int c=0;
cout<<c++<<c;

The answer says undefined. But in this case, isn't this statement
translated into:

cout.operator<<(c++).operator<<(c);

There should be a sequnce point before calling a funcion after
evaluation of the arguments. So shouldn't the incremented value of c be
stored before the call to first <<. If so it should be available for
the second <<, and the result would be 01.


There is a sequence point between the function calls, but not between
the evaluation of the arguments. That means, whether "c" is evaluated
before or after "c++" is undefined. Don't do that.
Jonathan

May 26 '06 #3
Could you please elaborate on this more verbosely? I understand that if
c++ is evaluated as an argument of a function and there's a sequence
point bofore calling this function the increment operator must be
executed and saved at this point. Because the dot operator bind from
left to right, the c should be incremented and saved so that it is
accessible in the second operarator<< call.

May 26 '06 #4
Are you sure it's the end of the function and not the so called
sequence point?

May 26 '06 #5
kw******@gmail.com wrote:
Could you please elaborate on this more verbosely? I understand that if
c++ is evaluated as an argument of a function and there's a sequence
point bofore calling this function the increment operator must be
executed and saved at this point.


Yes, but the other one can be executed already, too. It doesn't have to be
deferred to after the first function (or even after operator++) is called.
The compiler is free to first evaluate c, then c++, then call the first
operator<<, then the second one.
At least that's how I understand it.

May 26 '06 #6
kw******@gmail.com wrote:
Are you sure it's the end of the function and not the so called
sequence point?


Negative...I'll go back and read the standard...

Regards,
Ben
May 26 '06 #7
You seem to be right. Thank you for your explanation. I didn't think
about it that the two c's can be evaluated at any point.

May 26 '06 #8

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

Similar topics

2
by: Dave Theese | last post by:
Hello all, I have read the definition of a sequence point in the standard and can follow it mechanically, but for some reason, I'm having a hard time grasping the conceptual meaning and...
3
by: Sensorflo | last post by:
After browsing though many newsgroups articels I'm still not shure how operator precedence, operator associativity, sequence points, side effects go together. Currently I have the following view: ...
4
by: Timothy Madden | last post by:
Hello I've read a long time ago in the MSDN that C++ language defines no sequence points Now I read in the 1998 ISO standard a small list of sequence points in C++ Does C++ defines sequence...
53
by: Deniz Bahar | last post by:
I know the basic definition of a sequence point (point where all side effects guaranteed to be finished), but I am confused about this statement: "Between the previous and next sequence point an...
7
by: akarl | last post by:
Hi all, Why do I get a warning from gcc with the following program? $ cat test.c #include <stdio.h> int f(int n) { return n;
9
by: John Smith | last post by:
I've been playing with splint, which returns the following warning for the code below: statlib.c: (in function log_norm_pdf) statlib.c(1054,31): Expression has undefined behavior (left operand...
1
by: lovecreatesbea... | last post by:
---quoting--- Annex C (informative) Sequence points 1 The following are the sequence points described in 5.1.2.3: - The end of a full expression: an initializer (6.7.8); the expression in an...
4
by: Daniel Kraft | last post by:
Hi all! I do not have a standard-document right next to me to cite from, but as far as I know, doing something like: a()=b()=c()=d(); or foo(d()+c()+b()+a()); has a fixed evaluation order...
3
by: joe | last post by:
Consider the following program: include <iostream> class Bar { public: int getData9() { m_data = 9; return m_data;} int getData11() { m_data = 11; return m_data;} int m_data;
7
by: Jrdman | last post by:
hi According to the standard these are how we define sequence points: *the call to a function ,after the arguments have been evaluated *the end of the first operand of the following...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.