The inconsistent between BSD and c-faq on precedence

Discussion in 'C Programming' started by lovecreatesbea...@gmail.com, Oct 7, 2006.

  1. Guest

    c-faq, 4.3:
    The postfix ++ and -- operators essentially have higher precedence
    than the prefix unary operators.

    BSD Manual, OPERATOR(7):
    Operator Associativity
    -------- -------------
    ! ~ ++ -- - (type) * & sizeof right to left

    Is there a complete table on operators, their precedence and
    associativity in the standard document?
    , Oct 7, 2006
    #1
    1. Advertising

  2. said:

    <snip>

    > Is there a complete table on operators, their precedence and
    > associativity in the standard document?


    As has already been explained here very recently, the grammar determines how
    statements are parsed. The Standard does contain the grammar, of course.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Oct 7, 2006
    #2
    1. Advertising

  3. Guest

    Richard Heathfield wrote:
    > said:
    >
    > > Is there a complete table on operators, their precedence and
    > > associativity in the standard document?

    >
    > As has already been explained here very recently, the grammar determines how
    > statements are parsed. The Standard does contain the grammar, of course.


    Thank you. Could you please direct me to the grammar on precedence in
    the standard? For it is a little difficult on natural language
    (English, Chinese ...) reading for me to read through the whole
    document to look up one thing in it.

    I have found the similar inconsistent between K&R2 and H&S5 on the
    precedence.
    , Oct 7, 2006
    #3
  4. said:

    >
    > Richard Heathfield wrote:
    >> said:
    >>
    >> > Is there a complete table on operators, their precedence and
    >> > associativity in the standard document?

    >>
    >> As has already been explained here very recently, the grammar determines
    >> how statements are parsed. The Standard does contain the grammar, of
    >> course.

    >
    > Thank you. Could you please direct me to the grammar on precedence in
    > the standard?


    There isn't any "grammar on precedence". There's just grammar. The grammar
    defines how things are parsed. See Section 6.5 Expressions.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Oct 7, 2006
    #4
  5. jacob navia Guest

    wrote:
    > c-faq, 4.3:
    > The postfix ++ and -- operators essentially have higher precedence
    > than the prefix unary operators.
    >
    > BSD Manual, OPERATOR(7):
    > Operator Associativity
    > -------- -------------
    > ! ~ ++ -- - (type) * & sizeof right to left
    >
    > Is there a complete table on operators, their precedence and
    > associativity in the standard document?
    >



    According to Harbison and Steele, we have
    postfix ++ and postif -- with precedence 16

    Unary operators ~ , ! , have precedence 15,
    i.e. lower precedence, so the C Faq is right.

    You have propbably confused postfix ++ with
    *prefix* ++, that has the same precedence as the
    unary operators. Maybe, since this is not specified
    in the BSD table you showed, there is just
    a msunderstanding.
    jacob navia, Oct 7, 2006
    #5
  6. On 7 Oct 2006 06:38:50 -0700, ""
    <> wrote:

    >c-faq, 4.3:
    > The postfix ++ and -- operators essentially have higher precedence
    >than the prefix unary operators.


    Notice the word essentially. In this context, it means what follows
    is not precisely correct but has the same effect as if it were.

    >
    >BSD Manual, OPERATOR(7):
    > Operator Associativity
    > -------- -------------
    > ! ~ ++ -- - (type) * & sizeof right to left


    Consider the following code snippet inside a function:
    int i[2] = {3,4};
    int *p = i;
    int j = --*p++;

    Since --, *, and ++ have the same "official" precedence,
    associativity acts as a tie breaker. Thus, the right had side is
    evaluated as --(*(p++)). This is the same result you would get IF
    postfix had higher precedence than prefix.
    p++ evaluates to the current value of p and sometime before this
    statement is complete will increment p.
    *p++ evaluates to 3, the value of the int p initially pointed to.
    --*p++ evaluates to one less than this value and sometime before
    the statement is complete will decrement the value in its original
    location.

    When all is said and done, j is assigned the value 2, i[0] is
    decremented to 2, and p points to i[1] which is unchanged..

    >
    >Is there a complete table on operators, their precedence and
    >associativity in the standard document?


    If you have access to K&R, Table 2-1 on page 53 may be what you are
    looking for.


    Remove del for email
    Barry Schwarz, Oct 7, 2006
    #6
  7. wrote:
    > c-faq, 4.3:
    > The postfix ++ and -- operators essentially have higher precedence
    > than the prefix unary operators.
    >
    > BSD Manual, OPERATOR(7):
    > Operator Associativity
    > -------- -------------
    > ! ~ ++ -- - (type) * & sizeof right to left
    >
    > Is there a complete table on operators, their precedence and
    > associativity in the standard document?
    > ...


    The reason for this is that in C language where both prefix --/++ and
    postfix --/++ cannot be meaningfully applied to the same operand. The
    resulting code would produce undefined behavior anyway. For this reason,
    just to make the table a bit more compact, BSD manual does not separate
    --/++ into their prefix and postfix forms.

    The C FAQ approaches the same issue from more pedantic/theoretical point
    of view. According to C grammar, postfix --/++ operators have higher
    precedence than prefix --/++ operators.

    In C++ language, for example, this issue has practical value, since in
    general case overloaded --/++ operators can be meaningfully applied to
    the same operand. That's why in C++-specific precedence table you'll
    normally see prefix and postfix --/++ described separately. But in C
    there's no practical reason to do that.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Oct 8, 2006
    #7
  8. "" <> wrote in message
    news:...
    > c-faq, 4.3:
    > The postfix ++ and -- operators essentially have higher precedence
    > than the prefix unary operators.
    >
    > BSD Manual, OPERATOR(7):
    > Operator Associativity
    > -------- -------------
    > ! ~ ++ -- - (type) * & sizeof right to left
    >
    > Is there a complete table on operators, their precedence and
    > associativity in the standard document?
    >


    Sorry, no.

    "" <> wrote in message
    news:...
    > I have found the similar inconsistent between K&R2 and H&S5 on the
    > precedence.


    The first precedence table here is for K&R C:
    http://groups.google.com/group/comp.lang.c/msg/f17a5ff1a1783275?hl=en

    The second is for ANSI C 1989 (or ISO C 1990):
    http://groups.google.com/group/comp.lang.c/msg/5f46d39272cc3eec?hl=en

    Do the differences seem to correspond to the differences in K&R2 and H&S5?
    They do to me.



    Rod Pemberton
    Rod Pemberton, Oct 8, 2006
    #8
    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. j_mckitrick
    Replies:
    0
    Views:
    315
    j_mckitrick
    May 14, 2004
  2. Phil
    Replies:
    3
    Views:
    7,511
  3. Replies:
    9
    Views:
    1,027
  4. Chung Ta
    Replies:
    0
    Views:
    155
    Chung Ta
    Apr 22, 2004
  5. John
    Replies:
    14
    Views:
    302
Loading...

Share This Page