Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #14130 > unrolled thread

sequence points and printf()

Started byarnuld <sunrise@invalid.address>
First post2011-12-01 06:06 +0000
Last post2011-12-09 18:33 +0100
Articles 20 — 15 participants

Back to article view | Back to comp.lang.c


Contents

  sequence points and printf() arnuld <sunrise@invalid.address> - 2011-12-01 06:06 +0000
    Re: sequence points and printf() pacman@kosh.dhis.org (Alan Curry) - 2011-12-01 06:13 +0000
    Re: sequence points and printf() Martin Ambuhl <mambuhl@earthlink.net> - 2011-12-01 02:19 -0500
      Re: sequence points and printf() Keith Thompson <kst-u@mib.org> - 2011-12-01 06:55 -0800
        Re: sequence points and printf() pete <pfiland@mindspring.com> - 2011-12-01 11:01 -0500
    Re: sequence points and printf() "BartC" <bc@freeuk.com> - 2011-12-01 11:18 +0000
      Re: sequence points and printf() Keith Thompson <kst-u@mib.org> - 2011-12-01 07:03 -0800
      Re: sequence points and printf() Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-12-01 17:04 +0200
      Re: sequence points and printf() lawrence.jones@siemens.com - 2011-12-01 09:59 -0500
        Re: sequence points and printf() Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-01 20:48 -0500
      Re: sequence points and printf() Seebs <usenet-nospam@seebs.net> - 2011-12-01 20:08 +0000
    Re: sequence points and printf() Willem <willem@toad.stack.nl> - 2011-12-01 11:59 +0000
    Re: sequence points and printf() Seebs <usenet-nospam@seebs.net> - 2011-12-01 20:08 +0000
    Re: sequence points and printf() August Karlstrom <fusionfile@gmail.com> - 2011-12-01 23:17 +0100
      Re: sequence points and printf() Keith Thompson <kst-u@mib.org> - 2011-12-01 14:35 -0800
      Re: sequence points and printf() Kaz Kylheku <kaz@kylheku.com> - 2011-12-01 23:02 +0000
      Re: sequence points and printf() James Kuyper <jameskuyper@verizon.net> - 2011-12-01 20:13 -0500
        Re: sequence points and printf() Kaz Kylheku <kaz@kylheku.com> - 2011-12-02 02:09 +0000
          Re: sequence points and printf() August Karlstrom <fusionfile@gmail.com> - 2011-12-03 17:23 +0100
    Re: sequence points and printf() Vincenzo Mercuri <vincenzo.mercuri@hotmail.it> - 2011-12-09 18:33 +0100

#14130 — sequence points and printf()

Fromarnuld <sunrise@invalid.address>
Date2011-12-01 06:06 +0000
Subjectsequence points and printf()
Message-ID<4ed7195d$0$294$14726298@news.sunsite.dk>
OBJECTIVE: To know why its UB


#include <stdio.h>

int main(void)
{
  char c='8';
  int d=8;
  printf("%d %d %d\n",d, d+=c>='0'&& c<='9', c++);

  return 0;
}
================ OUTPUT ===================
[arnuld@dune C]$ gcc -ansi -pedantic -Wall -Wextra test.c
test.c: In function ‘main’:
test.c:7: warning: operation on ‘d’ may be undefined
test.c:7: warning: operation on ‘c’ may be undefined
[arnuld@dune C]$ ./a.out 
9 9 56
[arnuld@dune C]$ 


gcc says its UB. The way I understand it is:

 (1) d is incremented in 2nd argument without defining any sequence 
points, hence you can not guarantee what printf() will print as value of 
d.
  
 (2) same for c, as its being incremented without defining any sequence 
point.

But as per C-FAQs here:  http://c-faq.com/expr/seqpoints.html

comma operator defined a sequence point and I see comma is present 
between printf() arguments but I am not sure if this "comma" is called as 
"comma operator" ?
   

-- 
 arnuld
http://LispMachine.Wordpress.com

[toc] | [next] | [standalone]


#14131

Frompacman@kosh.dhis.org (Alan Curry)
Date2011-12-01 06:13 +0000
Message-ID<jb75tq$cjc$1@speranza.aioe.org>
In reply to#14130
In article <4ed7195d$0$294$14726298@news.sunsite.dk>,
arnuld  <sunrise@invalid.address> wrote:
>  printf("%d %d %d\n",d, d+=c>='0'&& c<='9', c++);
[...]
>
>comma operator defined a sequence point and I see comma is present 
>between printf() arguments but I am not sure if this "comma" is called as 
>"comma operator" ?

You hit on the problem right there. The function-argument-separating comma is
not an operator.

-- 
Alan Curry

[toc] | [prev] | [next] | [standalone]


#14133

FromMartin Ambuhl <mambuhl@earthlink.net>
Date2011-12-01 02:19 -0500
Message-ID<jb79pp$fdm$1@dont-email.me>
In reply to#14130
On 12/1/2011 1:06 AM, arnuld wrote:

>
> But as per C-FAQs here:  http://c-faq.com/expr/seqpoints.html
>
> comma operator defined a sequence point and I see comma is present
> between printf() arguments but I am not sure if this "comma" is called as
> "comma operator" ?

Commas between arguments are punctuation and not comma operators.

[toc] | [prev] | [next] | [standalone]


#14161

FromKeith Thompson <kst-u@mib.org>
Date2011-12-01 06:55 -0800
Message-ID<lnr50opciy.fsf@nuthaus.mib.org>
In reply to#14133
Martin Ambuhl <mambuhl@earthlink.net> writes:
> On 12/1/2011 1:06 AM, arnuld wrote:
>> But as per C-FAQs here:  http://c-faq.com/expr/seqpoints.html
>>
>> comma operator defined a sequence point and I see comma is present
>> between printf() arguments but I am not sure if this "comma" is called as
>> "comma operator" ?
>
> Commas between arguments are punctuation and not comma operators.

Right.

A terminology quibble: comma *operators* are also punctuation.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
                        Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#14168

Frompete <pfiland@mindspring.com>
Date2011-12-01 11:01 -0500
Message-ID<4ED7A4CC.4443@mindspring.com>
In reply to#14161
Keith Thompson wrote:
> 
> Martin Ambuhl <mambuhl@earthlink.net> writes:
> > On 12/1/2011 1:06 AM, arnuld wrote:
> >> But as per C-FAQs here:  http://c-faq.com/expr/seqpoints.html
> >>
> >> comma operator defined a sequence point and I see comma is present
> >> between printf() arguments
> >> but I am not sure if this "comma" is called as
> >> "comma operator" ?
> >
> > Commas between arguments are punctuation and not comma operators.
> 
> Right.
> 
> A terminology quibble: comma *operators* are also punctuation.

I think that Martin Ambuhl meant to say:

"Commas between arguments are punctuators and not comma operators."

-- 
pete

[toc] | [prev] | [next] | [standalone]


#14147

From"BartC" <bc@freeuk.com>
Date2011-12-01 11:18 +0000
Message-ID<jb7ns5$p8r$1@dont-email.me>
In reply to#14130

"arnuld" <sunrise@invalid.address> wrote in message 
news:4ed7195d$0$294$14726298@news.sunsite.dk...

>  printf("%d %d %d\n",d, d+=c>='0'&& c<='9', c++);

> gcc says its UB. The way I understand it is:

> comma operator defined a sequence point and I see comma is present
> between printf() arguments but I am not sure if this "comma" is called as
> "comma operator" ?

The problem here is that the order the printf arguments are evaluated, is 
not specified. The statement will give different results depending on 
whether they're evaluated left-to-right or right-to-left (or starting in the 
middle).

Saying it's 'UB' is a bit of an overreaction though, as the worst that can 
happen *ought* to be that wrong results are printed, and that d ends up 
being 1 more or less than you might expect.

-- 
Bartc 

[toc] | [prev] | [next] | [standalone]


#14162

FromKeith Thompson <kst-u@mib.org>
Date2011-12-01 07:03 -0800
Message-ID<lnmxbcpc5b.fsf@nuthaus.mib.org>
In reply to#14147
"BartC" <bc@freeuk.com> writes:
> "arnuld" <sunrise@invalid.address> wrote in message 
> news:4ed7195d$0$294$14726298@news.sunsite.dk...
>>  printf("%d %d %d\n",d, d+=c>='0'&& c<='9', c++);
>
>> gcc says its UB. The way I understand it is:
>
>> comma operator defined a sequence point and I see comma is present
>> between printf() arguments but I am not sure if this "comma" is called as
>> "comma operator" ?
>
> The problem here is that the order the printf arguments are evaluated, is 
> not specified. The statement will give different results depending on 
> whether they're evaluated left-to-right or right-to-left (or starting in the 
> middle).
>
> Saying it's 'UB' is a bit of an overreaction though, as the worst that can 
> happen *ought* to be that wrong results are printed, and that d ends up 
> being 1 more or less than you might expect.

No, the behavior is undefined.

C99 6.5p2:

    Between the previous and next sequence point an object shall
    have its stored value modified at most once by the evaluation
    of an expression. Furthermore, the prior value shall be read
    only to determine the value to be stored.

The above printf call doesn't violate the first sentence, but it
does violate the second.  There is no sequence point between reading
d and modifying d *and* the value read is not used to compute the
value to be stored.

Something like

    x = x + 1;

reads and writes x with no intervening sequence points, but its
behavior is well defined because the value that's read is used to
compute the value to be stored.  Because of this, the read *must*
occur before the write, so no conflict can occur.  (C201X expresses
this more clearly; it says that the read is "sequenced before"
the write, rather than leaving it implicit.)

In the printf call, the read and write of d are unsequenced
(can occur in either order), and the standard says this produces
undefined behavior.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#14163

FromPhil Carmody <thefatphil_demunged@yahoo.co.uk>
Date2011-12-01 17:04 +0200
Message-ID<87bors2v0f.fsf@bazspaz.fatphil.org>
In reply to#14147
"BartC" <bc@freeuk.com> writes:
> "arnuld" <sunrise@invalid.address> wrote in message
> news:4ed7195d$0$294$14726298@news.sunsite.dk...
> 
> >  printf("%d %d %d\n",d, d+=c>='0'&& c<='9', c++);
> 
> > gcc says its UB. The way I understand it is:
> 
> > comma operator defined a sequence point and I see comma is present
> > between printf() arguments but I am not sure if this "comma" is called as
> > "comma operator" ?
> 
> The problem here is that the order the printf arguments are evaluated,
> is not specified. The statement will give different results depending
> on whether they're evaluated left-to-right or right-to-left (or
> starting in the middle).
> 
> Saying it's 'UB' is a bit of an overreaction though, as the worst that
> can happen *ought* to be that wrong results are printed, and that d
> ends up being 1 more or less than you might expect.

This is a dangerous and sloppy way of thinking.

Arnuld is trying to learn C, we should be helping him to avoid
sloppy ways of thinking.

Anyway, I'd not say it's an over-reaction. An overreaction would be
someone going to their manager and saying something like "X is not
competent in the field for which he was hired. That's the second one
from the same consultancy in 6 months, can you tell resourcing there's
a problem?". Not that that's ever happened, nor did the one from 6
months earlier magically re-appear in a different team a year later.

Phil
-- 
Unix is simple. It just takes a genius to understand its simplicity
-- Dennis Ritchie (1941-2011), Unix Co-Creator

[toc] | [prev] | [next] | [standalone]


#14164

Fromlawrence.jones@siemens.com
Date2011-12-01 09:59 -0500
Message-ID<1chkq8-jig.ln1@jones.homeip.net>
In reply to#14147
BartC <bc@freeuk.com> wrote:
> 
> "arnuld" <sunrise@invalid.address> wrote in message 
> news:4ed7195d$0$294$14726298@news.sunsite.dk...
> 
> >  printf("%d %d %d\n",d, d+=c>='0'&& c<='9', c++);
> 
> The problem here is that the order the printf arguments are evaluated, is 
> not specified. The statement will give different results depending on 

Not just that, but there are no sequence points between the argument
evaluations.  Both c and d are modified as well as being accessed
outside the modification expression, which makes the behavior completely
undefined.
-- 
Larry Jones

It's clear I'll never have a career in sports until I learn
to suppress my survival instinct. -- Calvin

[toc] | [prev] | [next] | [standalone]


#14210

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2011-12-01 20:48 -0500
Message-ID<jb9aoj$846$2@dont-email.me>
In reply to#14164
On 12/1/2011 9:59 AM, lawrence.jones@siemens.com wrote:
> BartC<bc@freeuk.com>  wrote:
>>
>> "arnuld"<sunrise@invalid.address>  wrote in message
>> news:4ed7195d$0$294$14726298@news.sunsite.dk...
>>
>>>   printf("%d %d %d\n",d, d+=c>='0'&&  c<='9', c++);
>>
>> The problem here is that the order the printf arguments are evaluated, is
>> not specified. The statement will give different results depending on
>
> Not just that, but there are no sequence points between the argument
> evaluations.  Both c and d are modified as well as being accessed
> outside the modification expression, which makes the behavior completely
> undefined.

     As opposed to partially undefined?

     ;-)

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#14181

FromSeebs <usenet-nospam@seebs.net>
Date2011-12-01 20:08 +0000
Message-ID<slrnjdfos5.1m6m.usenet-nospam@guild.seebs.net>
In reply to#14147
On 2011-12-01, BartC <bc@freeuk.com> wrote:
> Saying it's 'UB' is a bit of an overreaction though, as the worst that can 
> happen *ought* to be that wrong results are printed, and that d ends up 
> being 1 more or less than you might expect.

Nope.  It's undefined behavior, full stop.

And yes, I have seen REALLY crazy results from stuff like this.  I once
saw someone do something roughly to the effect of:

	i = ++i % 4;

and get results which followed no particular pattern I could understand,
but they got larger and larger -- say, well over 1000.

No idea why.  Changed the code, problem went away, not worth trying to figure
it out.

-s
-- 
Copyright 2011, all wrongs reversed.  Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

[toc] | [prev] | [next] | [standalone]


#14149

FromWillem <willem@toad.stack.nl>
Date2011-12-01 11:59 +0000
Message-ID<slrnjder0i.qjf.willem@toad.stack.nl>
In reply to#14130
arnuld wrote:
) OBJECTIVE: To know why its UB
...
)   printf("%d %d %d\n",d, d+=c>='0'&& c<='9', c++);
...
) comma operator defined a sequence point and I see comma is present 
) between printf() arguments but I am not sure if this "comma" is called as 
) "comma operator" ?

This "comma" is definitely not called as "comma operator".

HTH, HAND.


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

[toc] | [prev] | [next] | [standalone]


#14180

FromSeebs <usenet-nospam@seebs.net>
Date2011-12-01 20:08 +0000
Message-ID<slrnjdfope.1m6m.usenet-nospam@guild.seebs.net>
In reply to#14130
On 2011-12-01, arnuld <sunrise@invalid.address> wrote:
> comma operator defined a sequence point and I see comma is present 
> between printf() arguments but I am not sure if this "comma" is called as 
> "comma operator" ?

It's not.  The commas separating function call arguments are not
comma operators.

-s
-- 
Copyright 2011, all wrongs reversed.  Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

[toc] | [prev] | [next] | [standalone]


#14192

FromAugust Karlstrom <fusionfile@gmail.com>
Date2011-12-01 23:17 +0100
Message-ID<jb8uds$tl8$1@dont-email.me>
In reply to#14130
On 2011-12-01 07:06, arnuld wrote:
[...]
> But as per C-FAQs here:  http://c-faq.com/expr/seqpoints.html
>
> comma operator defined a sequence point and I see comma is present
> between printf() arguments but I am not sure if this "comma" is called as
> "comma operator" ?

Just want to add that unless you are into code maintenance I see no 
point in bothering with sequence points.

August

[toc] | [prev] | [next] | [standalone]


#14195

FromKeith Thompson <kst-u@mib.org>
Date2011-12-01 14:35 -0800
Message-ID<ln1usnq5rr.fsf@nuthaus.mib.org>
In reply to#14192
August Karlstrom <fusionfile@gmail.com> writes:
> On 2011-12-01 07:06, arnuld wrote:
> [...]
>> But as per C-FAQs here:  http://c-faq.com/expr/seqpoints.html
>>
>> comma operator defined a sequence point and I see comma is present
>> between printf() arguments but I am not sure if this "comma" is called as
>> "comma operator" ?
>
> Just want to add that unless you are into code maintenance I see no 
> point in bothering with sequence points.

How does one not bother with sequence points?

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#14198

FromKaz Kylheku <kaz@kylheku.com>
Date2011-12-01 23:02 +0000
Message-ID<20111201150049.832@kylheku.com>
In reply to#14192
On 2011-12-01, August Karlstrom <fusionfile@gmail.com> wrote:
> On 2011-12-01 07:06, arnuld wrote:
> [...]
>> But as per C-FAQs here:  http://c-faq.com/expr/seqpoints.html
>>
>> comma operator defined a sequence point and I see comma is present
>> between printf() arguments but I am not sure if this "comma" is called as
>> "comma operator" ?
>
> Just want to add that unless you are into code maintenance I see no 
> point in bothering with sequence points.

Phew! I always just write new code myself and let others debug/maintain it, so
I'm safe from sequence points, I guess.

:)

[toc] | [prev] | [next] | [standalone]


#14206

FromJames Kuyper <jameskuyper@verizon.net>
Date2011-12-01 20:13 -0500
Message-ID<jb98o5$u0b$1@dont-email.me>
In reply to#14192
On 12/01/2011 05:17 PM, August Karlstrom wrote:
> On 2011-12-01 07:06, arnuld wrote:
> [...]
>> But as per C-FAQs here:  http://c-faq.com/expr/seqpoints.html
>>
>> comma operator defined a sequence point and I see comma is present
>> between printf() arguments but I am not sure if this "comma" is called as
>> "comma operator" ?
> 
> Just want to add that unless you are into code maintenance I see no 
> point in bothering with sequence points.

I'm curious - if you don't bother with sequence points, how do you write
C? Any C program has a minimum of at least one sequence point.

Or are you saying you see no point in bothering to understand sequence
points? In that case, how do you avoid writing code that has undefined
behavior by reason of not bothering with sequence points?

I suppose a sufficient broad definition of "code maintenance" might turn
that into a reasonable assertion - but it would have to be broad enough
to include all writing of C code with desire to have create a program
that functions correctly.
-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#14211

FromKaz Kylheku <kaz@kylheku.com>
Date2011-12-02 02:09 +0000
Message-ID<20111201173215.831@kylheku.com>
In reply to#14206
On 2011-12-02, James Kuyper <jameskuyper@verizon.net> wrote:
> On 12/01/2011 05:17 PM, August Karlstrom wrote:
>> On 2011-12-01 07:06, arnuld wrote:
>> [...]
>>> But as per C-FAQs here:  http://c-faq.com/expr/seqpoints.html
>>>
>>> comma operator defined a sequence point and I see comma is present
>>> between printf() arguments but I am not sure if this "comma" is called as
>>> "comma operator" ?
>> 
>> Just want to add that unless you are into code maintenance I see no 
>> point in bothering with sequence points.
>
> I'm curious - if you don't bother with sequence points, how do you write
> C? Any C program has a minimum of at least one sequence point.
>
> Or are you saying you see no point in bothering to understand sequence
> points? In that case, how do you avoid writing code that has undefined
> behavior by reason of not bothering with sequence points?

You can avoid knowledge of sequence points by memorizing some simplified
coding rules instead: in any full expression (such as an expression
statement, initializer, or the controlling expression of a loop, etc) never
modify the same object twice, and never make an ambiguous-looking access to an
object that is being modified (any access to an object being modified
has to be one of the operands of the expressions which compute the new value
being stored).

[toc] | [prev] | [next] | [standalone]


#14264

FromAugust Karlstrom <fusionfile@gmail.com>
Date2011-12-03 17:23 +0100
Message-ID<jbdida$fmr$1@dont-email.me>
In reply to#14211
On 2011-12-02 03:09, Kaz Kylheku wrote:
> You can avoid knowledge of sequence points by memorizing some simplified
> coding rules instead: in any full expression (such as an expression
> statement, initializer, or the controlling expression of a loop, etc) never
> modify the same object twice, and never make an ambiguous-looking access to an
> object that is being modified (any access to an object being modified
> has to be one of the operands of the expressions which compute the new value
> being stored).

Yes, that is what I meant.

August

[toc] | [prev] | [next] | [standalone]


#14424

FromVincenzo Mercuri <vincenzo.mercuri@hotmail.it>
Date2011-12-09 18:33 +0100
Message-ID<FYGdnS6E5ebJ23_TnZ2dnUVZ8nqdnZ2d@giganews.com>
In reply to#14130
arnuld ha scritto:
...
> #include<stdio.h>
>
> int main(void)
> {
>    char c='8';
>    int d=8;
>    printf("%d %d %d\n",d, d+=c>='0'&&  c<='9', c++);
>
>    return 0;
> }
...
> comma operator defined a sequence point and I see comma is present
> between printf() arguments but I am not sure if this "comma" is called as
> "comma operator" ?
...

The only way to turn those commas into "comma operators" is by
putting some extra parenthes. In the following (awful) program,
the first comma is an operator, the second comma just separates
the two arguments passed to the inner printf(), the third comma
is also a comma operator, the fourth comma separates the two
arguments passed to the outer printf():


#include <stdio.h>

int main(void)
{
   int d = 0;
   printf( (d++, printf("%d\n", d), "%s\n"), "done" );
   return 0;
}


-- 
Vincenzo Mercuri | www.backbox.org

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web