I know there's a sequence point at a comma, e.g.:
int main(void)
{
int a = 1;
a++, ++a, a *= 3, a <<= 4; /* Perfectly okay */
}
But does that also include the commas which separate function arguments? Is
the following code broken?
void Func( int, int, int, int );
int main(void)
{
int a = 1;
Func( a++, ++a, a *= 3, a <<= 4 );
}
--
Frederick Gotham 17 1893
Frederick Gotham schrieb: I know there's a sequence point at a comma, e.g.:
int main(void) { int a = 1;
a++, ++a, a *= 3, a <<= 4; /* Perfectly okay */ }
But does that also include the commas which separate function arguments?
No.
Is the following code broken?
void Func( int, int, int, int );
int main(void) { int a = 1;
Func( a++, ++a, a *= 3, a <<= 4 ); }
Yes.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
On Wed, 28 Jun 2006 16:12:36 GMT, Frederick Gotham
<fg*******@SPAM.com> wrote: I know there's a sequence point at a comma, e.g.:
int main(void) { int a = 1;
a++, ++a, a *= 3, a <<= 4; /* Perfectly okay */ }
But does that also include the commas which separate function arguments? Is the following code broken?
void Func( int, int, int, int );
int main(void) { int a = 1;
Func( a++, ++a, a *= 3, a <<= 4 ); }
Yes, it is broken:
ISO/IEC 9899:1999
6.5.2.2 Function calls
Constraints
....
10 The order of evaluation of the function designator, the actual
arguments, and subexpressions within the actual arguments is
unspecified, but there is a sequence point before the actual call.
Frederick Gotham wrote: I know there's a sequence point at a comma, e.g.:
int main(void) { int a = 1;
a++, ++a, a *= 3, a <<= 4; /* Perfectly okay */ }
Yes. Not only is there a sequence point between each operation but the
order of evaluation is guaranteed to be from left to right, the result
of which will have the type and valud of the rightmost expression.
But does that also include the commas which separate function arguments?
No, commas seperating function arguments or list items are not the
comma operator and do not introduce a sequence point.
Is the following code broken?
Yes.
void Func( int, int, int, int );
int main(void) { int a = 1;
Func( a++, ++a, a *= 3, a <<= 4 ); }
The function arguments may be evaluated in any order and since there is
no sequence point between the modifications of "a" the behavior is
undefined.
Robert Gamble
Frederick Gotham wrote: a++, ++a, a *= 3, a <<= 4; /* Perfectly okay */
There is a difference between "it'll work" and "Perfectly okay".
Good: knowing about sequence points
Bad: Thinking the coma operator is a license to write anything.
Tom
On Wed, 28 Jun 2006 16:12:36 GMT, Frederick Gotham
<fg*******@SPAM.com> wrote: I know there's a sequence point at a comma, e.g.:
No, there's a sequence point at comma operators, not at commas in
general.
int main(void) { int a = 1;
a++, ++a, a *= 3, a <<= 4; /* Perfectly okay */ }
But does that also include the commas which separate function arguments? Is the following code broken?
void Func( int, int, int, int );
int main(void) { int a = 1;
Func( a++, ++a, a *= 3, a <<= 4 ); }
--
Al Balmer
Sun City, AZ
"Tom St Denis" <to********@gmail.com> writes: Frederick Gotham wrote: a++, ++a, a *= 3, a <<= 4; /* Perfectly okay */
There is a difference between "it'll work" and "Perfectly okay".
Good: knowing about sequence points
Bad: Thinking the coma operator is a license to write anything.
The coma operator is a license to take a very long nap.
Getting back to the comma operator ... 8-)}
The above code fragment is ok for illustrating the point. It should
not appear in actual code. A better way to write it would be:
a = ((a + 2) * 3) << 4;
More generally, if the subexpressions can't be combined like this,
there's no good reason to use a comma operator rather than separate
statements:
a++;
++b;
c *= 3;
d <<= 4;
*unless* the whole thing is part of a macro definition that needs to
expand to an expression rather than a sequence of statements.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson wrote:
"Tom St Denis" <to********@gmail.comwrites:
Frederick Gotham wrote:
a++, ++a, a *= 3, a <<= 4; /* Perfectly okay */
There is a difference between "it'll work" and "Perfectly okay".
Good: knowing about sequence points
Bad: Thinking the coma operator is a license to write anything.
The coma operator is a license to take a very long nap.
Getting back to the comma operator ... 8-)}
The above code fragment is ok for illustrating the point. It should
not appear in actual code. A better way to write it would be:
a = ((a + 2) * 3) << 4;
More generally, if the subexpressions can't be combined like this,
there's no good reason to use a comma operator rather than separate
statements:
a++;
++b;
c *= 3;
d <<= 4;
*unless* the whole thing is part of a macro definition that needs to
expand to an expression rather than a sequence of statements.
The statement that "there is no good reason" is a
little strong. I expect you've seen otherwise
reasonable code that puts related assignments on the
same line, using comma operators to separate them:
p++, q++;
Depending on circumstances, it can help code clarity
to do so. cf Fred Brooks, "The Mythical Man-Month". en******@yahoo.com writes:
Keith Thompson wrote:
[...]
>More generally, if the subexpressions can't be combined like this, there's no good reason to use a comma operator rather than separate statements:
a++; ++b; c *= 3; d <<= 4;
*unless* the whole thing is part of a macro definition that needs to expand to an expression rather than a sequence of statements.
The statement that "there is no good reason" is a
little strong. I expect you've seen otherwise
reasonable code that puts related assignments on the
same line, using comma operators to separate them:
p++, q++;
Depending on circumstances, it can help code clarity
to do so. cf Fred Brooks, "The Mythical Man-Month".
If I felt the need to put those two expressions on the same line, I'd
probably write
p++; q++;
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson wrote:
en******@yahoo.com writes:
Keith Thompson wrote:
[...]
More generally, if the subexpressions can't be combined like this,
there's no good reason to use a comma operator rather than separate
statements:
a++;
++b;
c *= 3;
d <<= 4;
*unless* the whole thing is part of a macro definition that needs to
expand to an expression rather than a sequence of statements.
The statement that "there is no good reason" is a
little strong. I expect you've seen otherwise
reasonable code that puts related assignments on the
same line, using comma operators to separate them:
p++, q++;
Depending on circumstances, it can help code clarity
to do so. cf Fred Brooks, "The Mythical Man-Month".
If I felt the need to put those two expressions on the same line, I'd
probably write
p++; q++;
Yes that's another way of doing it. I usually
follow the rule of no more than one statement
on a line, which fits nicely with how comma
works in C.
Keith Thompson posted:
>If I felt the need to put those two expressions on the same line, I'd probably write
p++; q++;
If I have a very simple if-statement or loop, I might write:
if(expr) ++p,++q;
do ++p,++q;
while (expr);
--
Frederick Gotham
"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:uH*******************@news.indigo.ie...
Keith Thompson posted:
>>If I felt the need to put those two expressions on the same line, I'd probably write
p++; q++;
If I have a very simple if-statement or loop, I might write:
if(expr) ++p,++q;
do ++p,++q;
while (expr);
--
Frederick Gotham
I find those constructs to be inherently dangerous -
someone who dislikes using comma operators may
later come along and replace the commas
with semicolons, and bang! it's broken.
My personal coding standard is to ALWAYS use
braces in if, for, do, and while expressions.Even:
if (something) {return;}
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
On Tue, 18 Jul 2006 09:13:15 GMT, Keith Thompson <ks***@mib.org>
wrote:
>en******@yahoo.com writes:
>Keith Thompson wrote:
[...]
>>More generally, if the subexpressions can't be combined like this, there's no good reason to use a comma operator rather than separate statements:
a++; ++b; c *= 3; d <<= 4;
*unless* the whole thing is part of a macro definition that needs to expand to an expression rather than a sequence of statements.
The statement that "there is no good reason" is a little strong. I expect you've seen otherwise reasonable code that puts related assignments on the same line, using comma operators to separate them:
p++, q++;
Depending on circumstances, it can help code clarity to do so. cf Fred Brooks, "The Mythical Man-Month".
If I felt the need to put those two expressions on the same line, I'd probably write
p++; q++;
Not where I've usually seen such things - as the third expression in a
for statement.
--
Al Balmer
Sun City, AZ
Al Balmer <al******@att.netwrites:
On Tue, 18 Jul 2006 09:13:15 GMT, Keith Thompson <ks***@mib.org>
wrote:
>>en******@yahoo.com writes:
>>Keith Thompson wrote:
[...]
>>>More generally, if the subexpressions can't be combined like this, there's no good reason to use a comma operator rather than separate statements:
a++; ++b; c *= 3; d <<= 4;
*unless* the whole thing is part of a macro definition that needs to expand to an expression rather than a sequence of statements.
The statement that "there is no good reason" is a little strong. I expect you've seen otherwise reasonable code that puts related assignments on the same line, using comma operators to separate them:
p++, q++;
Depending on circumstances, it can help code clarity to do so. cf Fred Brooks, "The Mythical Man-Month".
If I felt the need to put those two expressions on the same line, I'd probably write
p++; q++;
Not where I've usually seen such things - as the third expression in a
for statement.
Yes, that's another reasonable use for the comma operator, and one
that I forgot to mention.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson wrote:
en******@yahoo.com writes:
>Keith Thompson wrote:
[...]
>>More generally, if the subexpressions can't be combined like this, there's no good reason to use a comma operator rather than separate statements:
a++; ++b; c *= 3; d <<= 4;
*unless* the whole thing is part of a macro definition that needs to expand to an expression rather than a sequence of statements.
The statement that "there is no good reason" is a little strong. I expect you've seen otherwise reasonable code that puts related assignments on the same line, using comma operators to separate them:
p++, q++;
Depending on circumstances, it can help code clarity to do so. cf Fred Brooks, "The Mythical Man-Month".
If I felt the need to put those two expressions on the same line, I'd
probably write
p++; q++;
The only place I use the comma operator is where two expressions are to
form a single statement.
char *fn = "filename";
if ((fp = fopen(fn, "r")) == NULL)
fprintf(stderr, "Can't open %s\n", fn), exit(EXIT_FAILURE);
Avoiding the comma will create two statements and cost a pair of curlies
as well.
if ((fp = fopen(fn, "r")) == NULL) {
fprintf(stderr, "Can't open %s\n", fn);
exit(EXIT_FAILURE);
}
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Joe Wright wrote:
The only place I use the comma operator
is where two expressions are to
form a single statement.
char *fn = "filename";
if ((fp = fopen(fn, "r")) == NULL)
fprintf(stderr, "Can't open %s\n", fn), exit(EXIT_FAILURE);
Avoiding the comma will create two statements
and cost a pair of curlies as well.
if ((fp = fopen(fn, "r")) == NULL) {
fprintf(stderr, "Can't open %s\n", fn);
exit(EXIT_FAILURE);
}
I would write it the second way
without even thinking about it.
I propbably don't fully appreciate
the cost of a pair of curlies.
--
pete
Joe Wright <jo********@comcast.netwrites:
Keith Thompson wrote:
>en******@yahoo.com writes:
>>Keith Thompson wrote:
[...]
>>>More generally, if the subexpressions can't be combined like this, there's no good reason to use a comma operator rather than separate statements:
a++; ++b; c *= 3; d <<= 4;
*unless* the whole thing is part of a macro definition that needs to expand to an expression rather than a sequence of statements. The statement that "there is no good reason" is a little strong. I expect you've seen otherwise reasonable code that puts related assignments on the same line, using comma operators to separate them:
p++, q++;
Depending on circumstances, it can help code clarity to do so. cf Fred Brooks, "The Mythical Man-Month".
If I felt the need to put those two expressions on the same line, I'd probably write p++; q++;
The only place I use the comma operator is where two expressions are
to form a single statement.
char *fn = "filename";
if ((fp = fopen(fn, "r")) == NULL)
fprintf(stderr, "Can't open %s\n", fn), exit(EXIT_FAILURE);
Avoiding the comma will create two statements and cost a pair of
curlies as well.
if ((fp = fopen(fn, "r")) == NULL) {
fprintf(stderr, "Can't open %s\n", fn);
exit(EXIT_FAILURE);
}
Yes, at the terrible expense of code that's much clearer and easier to
read.
It wouldn't even occur to me to use a comma operator in that case.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson wrote:
Joe Wright <jo********@comcast.netwrites:
>Keith Thompson wrote:
>>en******@yahoo.com writes: Keith Thompson wrote: [...] More generally, if the subexpressions can't be combined like this, there's no good reason to use a comma operator rather than separate statements: > a++; ++b; c *= 3; d <<= 4; > *unless* the whole thing is part of a macro definition that needs to expand to an expression rather than a sequence of statements. The statement that "there is no good reason" is a little strong. I expect you've seen otherwise reasonable code that puts related assignments on the same line, using comma operators to separate them:
p++, q++;
Depending on circumstances, it can help code clarity to do so. cf Fred Brooks, "The Mythical Man-Month". If I felt the need to put those two expressions on the same line, I'd probably write p++; q++;
The only place I use the comma operator is where two expressions are to form a single statement.
char *fn = "filename";
if ((fp = fopen(fn, "r")) == NULL) fprintf(stderr, "Can't open %s\n", fn), exit(EXIT_FAILURE);
Avoiding the comma will create two statements and cost a pair of curlies as well.
if ((fp = fopen(fn, "r")) == NULL) { fprintf(stderr, "Can't open %s\n", fn); exit(EXIT_FAILURE); }
Yes, at the terrible expense of code that's much clearer and easier to
read.
It wouldn't even occur to me to use a comma operator in that case.
A chacun son gout. I kinda like it. :-)
Your disapproval is noted. And pete's too.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein --- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dave Moore |
last post by:
Is there any way to specify an istream separator sequence? For example,
suppose I have a record consisting of a list of comma-separated values (no
whitespace). I want to set the istream up so that...
|
by: sugaray |
last post by:
Can somebody explain to me what is sequence point ?
With few examples would be even better. Thanx for your help.
|
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...
|
by: Senthil |
last post by:
Hi,
When standard states this
"
i = v; // the behavior is undefined
i = ++i + 1; // the behavior is undefined
"
Do both of the following code snippets produces undefined behaviour or
the...
|
by: Frederick Gotham |
last post by:
Here's a sample function which converts a string to all uppercase:
#include <cassert>
#include <cctype>
void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p = std::toupper( *p ), *p++...
| |
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...
|
by: asearle |
last post by:
Hi everyone,
I am on a project here in Germany and am having a tricky problem with decimal points which in Germany (and most other European countries) are represented by a comma instead.
My...
|
by: Kai-Uwe Bux |
last post by:
Please consider
#include <iostream>
int main ( void ) {
int a = 0;
a = ( (a = 5), 4 ); // (*)
std::cout << a << '\n';
}
|
by: somenath |
last post by:
Hi All,
I have one question regarding the conditional operator.
In the draft C99 standard it is mentioned that
"1 The following are the sequence points described in 5.1.2.3:
-- The call to a...
|
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: 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...
| |
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: 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...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |