The inconsistent between BSD and c-faq on precedence

  • Thread starter lovecreatesbea...
  • Start date
L

lovecreatesbea...

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?
 
R

Richard Heathfield

(e-mail address removed) 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.
 
L

lovecreatesbea...

Richard said:
(e-mail address removed) said:


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.
 
R

Richard Heathfield

(e-mail address removed) said:
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.
 
J

jacob navia

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.
 
B

Barry Schwarz

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
 
A

Andrey Tarasevich

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.
 
R

Rod Pemberton

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.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top