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


Groups > comp.lang.c > #14162

Re: sequence points and printf()

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: sequence points and printf()
Date 2011-12-01 07:03 -0800
Organization None to speak of
Message-ID <lnmxbcpc5b.fsf@nuthaus.mib.org> (permalink)
References <4ed7195d$0$294$14726298@news.sunsite.dk> <jb7ns5$p8r$1@dont-email.me>

Show all headers | View raw


"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"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web