Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > The inconsistent between BSD and c-faq on precedence

Reply
Thread Tools

The inconsistent between BSD and c-faq on precedence

 
 
lovecreatesbea...@gmail.com
Guest
Posts: n/a
 
      10-07-2006
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?

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      10-07-2006
(E-Mail Removed) 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)
 
Reply With Quote
 
 
 
 
lovecreatesbea...@gmail.com
Guest
Posts: n/a
 
      10-07-2006

Richard Heathfield wrote:
> (E-Mail 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.


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.

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      10-07-2006
(E-Mail Removed) said:

>
> Richard Heathfield wrote:
>> (E-Mail 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.

>
> 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)
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      10-07-2006
(E-Mail Removed) 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.
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      10-07-2006
On 7 Oct 2006 06:38:50 -0700, "(E-Mail Removed)"
<(E-Mail Removed)> 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
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      10-08-2006
(E-Mail Removed) 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

 
Reply With Quote
 
Rod Pemberton
Guest
Posts: n/a
 
      10-08-2006

"(E-Mail Removed)" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) s.com...
> 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.

"(E-Mail Removed)" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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....a1783275?hl=en

The second is for ANSI C 1989 (or ISO C 1990):
http://groups.google.com/group/comp....72cc3eec?hl=en

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



Rod Pemberton


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
IPSec tunnel between ASA and *BSD m.mazurek@netsync.pl Cisco 0 05-26-2007 06:13 PM
precedence between OnClientClick and OnClick? Phil ASP .Net 3 03-08-2007 08:38 PM
Inconsistent behavior between SQL*Plus and Perl DBI John Perl Misc 14 07-23-2004 03:46 PM
Inconsistent File Access problem between asp.net and interally invoked console app Chung Ta ASP .Net Security 0 04-22-2004 02:56 PM
F5 BigIP and 3DNS are running on BSD/OS and BSD/OS is discontinued Baby Peanut Cisco 0 09-11-2003 12:55 AM



Advertisments