sequence points and printf()

Discussion in 'C Programming' started by arnuld, Dec 1, 2011.

  1. arnuld

    arnuld Guest

    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
     
    arnuld, Dec 1, 2011
    #1
    1. Advertising

  2. arnuld

    Alan Curry Guest

    In article <4ed7195d$0$294$>,
    arnuld <> 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
     
    Alan Curry, Dec 1, 2011
    #2
    1. Advertising

  3. 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.
     
    Martin Ambuhl, Dec 1, 2011
    #3
  4. arnuld

    BartC Guest

    "arnuld" <> wrote in message
    news:4ed7195d$0$294$...

    > 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
     
    BartC, Dec 1, 2011
    #4
  5. arnuld

    Willem Guest

    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
     
    Willem, Dec 1, 2011
    #5
  6. Martin Ambuhl <> 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) <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"
     
    Keith Thompson, Dec 1, 2011
    #6
  7. arnuld

    Guest

    BartC <> wrote:
    >
    > "arnuld" <> wrote in message
    > news:4ed7195d$0$294$...
    >
    > > 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
     
    , Dec 1, 2011
    #7
  8. "BartC" <> writes:
    > "arnuld" <> wrote in message
    > news:4ed7195d$0$294$...
    >> 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) <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"
     
    Keith Thompson, Dec 1, 2011
    #8
  9. arnuld

    Phil Carmody Guest

    "BartC" <> writes:
    > "arnuld" <> wrote in message
    > news:4ed7195d$0$294$...
    >
    > > 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
     
    Phil Carmody, Dec 1, 2011
    #9
  10. arnuld

    Seebs Guest

    On 2011-12-01, arnuld <> 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 /
    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.
     
    Seebs, Dec 1, 2011
    #10
  11. arnuld

    Seebs Guest

    On 2011-12-01, BartC <> 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 /
    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.
     
    Seebs, Dec 1, 2011
    #11
  12. 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
     
    August Karlstrom, Dec 1, 2011
    #12
  13. August Karlstrom <> 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) <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"
     
    Keith Thompson, Dec 1, 2011
    #13
  14. arnuld

    Kaz Kylheku Guest

    On 2011-12-01, 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.


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

    :)
     
    Kaz Kylheku, Dec 1, 2011
    #14
  15. arnuld

    James Kuyper Guest

    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
     
    James Kuyper, Dec 2, 2011
    #15
  16. arnuld

    Eric Sosman Guest

    On 12/1/2011 9:59 AM, wrote:
    > BartC<> wrote:
    >>
    >> "arnuld"<> wrote in message
    >> news:4ed7195d$0$294$...
    >>
    >>> 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
    d
     
    Eric Sosman, Dec 2, 2011
    #16
  17. arnuld

    Kaz Kylheku Guest

    On 2011-12-02, James Kuyper <> 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).
     
    Kaz Kylheku, Dec 2, 2011
    #17
  18. 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
     
    August Karlstrom, Dec 3, 2011
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Smith

    sequence points and evaluation order

    John Smith, Sep 4, 2006, in forum: C Programming
    Replies:
    9
    Views:
    350
    Robert Gamble
    Sep 6, 2006
  2. Divick
    Replies:
    2
    Views:
    322
    Divick
    Sep 5, 2006
  3. Dave Rahardja
    Replies:
    8
    Views:
    409
    Dave Rahardja
    Jul 5, 2007
  4. ais523
    Replies:
    9
    Views:
    370
    Kaz Kylheku
    Feb 9, 2008
  5. George

    sequence points and the execution model

    George, Jan 17, 2009, in forum: C Programming
    Replies:
    7
    Views:
    322
    Richard
    Jan 20, 2009
Loading...

Share This Page