Linux-man Archive mirror
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx@kernel.org>
To: linux-man@vger.kernel.org
Cc: lijh8 <lijh8@qq.com>
Subject: Re: FWD: lijh8@qq.com: about operator(7) precedence
Date: Fri, 19 Jan 2024 23:58:24 +0100	[thread overview]
Message-ID: <Zar-liK0Ev692sTQ@debian> (raw)
In-Reply-To: <ZaruodlIbZHnErOK@debian>

[-- Attachment #1: Type: text/plain, Size: 5591 bytes --]

On Fri, Jan 19, 2024 at 10:50:19PM +0100, Alejandro Colomar wrote:
> ----- Forwarded message from lijh8 <lijh8@qq.com> -----
> 
> Date: Sat, 20 Jan 2024 05:20:40 +0800
> From: lijh8 <lijh8@qq.com>
> To: alx <alx@kernel.org>
> Cc: linux-man <linux-man@vger.kernel.org>
> Subject: about operator(7) precedence
> Message-ID: <tencent_98F329827330C28C2727E9F2106379868609@qq.com>
> X-Mailer: QQMail 2.x
> 

I'll reorder your mail, for a better reply.

> What's the reason, is the C language changed?

The C language specifies operators in 6.5 (Expressions).
<http://port70.net/~nsz/c/c11/n1570.html#6.5>

There, there's a note, 85, that says that

	The syntax specifies the precedence of operators in the
	evaluation of an expression, which is the same as the order of
	the major subclauses of this subclause, highest precedence first.
	...
<http://port70.net/~nsz/c/c11/n1570.html#note85>

Postfix ++ and -- are described in 6.5.2.4, that is, in the second group
of operators, sorted by precedence (the first is _Generic(), which isn't
documented in the manual pages).
<http://port70.net/~nsz/c/c11/n1570.html#6.5.2.4>

Prefix ++ and -- are described in 6.5.3.1, that is, in the third group
of operators, sorted by precedence.
<http://port70.net/~nsz/c/c11/n1570.html#6.5.3.1>

> Why on linux, postfix and prefix are divided into two different 
> precedences?

The Linux manual page is correct, closely following what ISO C says (I
showed C11, but I'm sure that all other versions of ISO C have the same
precedence rules).

> Hi, on macOS (and maybe freeBSD), postfix and prefix ++ -- are the same 
> precedence and can be distinguished by associativities.

Since x--++ is not legal, because x-- is not an lvalue, there's probably
no real difference in the two different models of the same reality.

However, pedantically, the macOS manual page is lying.  I've checked,
and the FreeBSD and OpenBSD manual pages also lie about this.

<https://man.openbsd.org/operator.7>
<https://man.freebsd.org/cgi/man.cgi?query=operator&manpath=FreeBSD+14.0-RELEASE+and+Ports>

Have a lovely day,
Alex

> 
> 
> ---
> 
> 
> ```
> 
> 
> OPERATOR(7)             Miscellaneous Information Manual            OPERATOR(7)
> 
> 
> NAME
>      operator – C operator precedence and order of evaluation
> 
> 
> DESCRIPTION
>            Operator                        Associativity
>            --------                        -------------
>            () [] -> .                      left to right
>            ! ~ ++ -- - (type) * & sizeof   right to left
>            * / %                           left to right
>            + -                             left to right
>            << >>                           left to right
>            < <= > >=                       left to right
>            == !=                           left to right
>            &                               left to right
>            ^                               left to right
>            |                               left to right
>            &&                              left to right
>            ||                              left to right
>            ?:                              right to left
>            = += -= etc.                    right to left
>            ,                               left to right
> 
> 
> FILES
>      /usr/share/misc/operator
> 
> 
> macOS 12.7                        June 9, 1993                       macOS 12.7
> 
> 
> ```
> 
> 
> ---
> 
> 
> ```
> 
> 
> operator(7)            Miscellaneous Information Manual           operator(7)
> 
> 
> NAME
>        operator - C operator precedence and order of evaluation
> 
> 
> DESCRIPTION
>        This manual page lists C operators and their precedence in evaluation.
> 
> 
>        Operator                            Associativity   Notes
>        [] () . -> ++ --                    left to right   [1]
>        ++ -- & * + - ~ ! sizeof            right to left   [2]
>        (type)                              right to left
>        * / %                               left to right
>        + -                                 left to right
>        << >>                               left to right
>        < > <= >=                           left to right
>        == !=                               left to right
>        &                                   left to right
>        ^                                   left to right
>        |                                   left to right
>        &&                                  left to right
>        ||                                  left to right
>        ?:                                  right to left
>        = *= /= %= += -= <<= >>= &= ^= |=   right to left
>        ,                                   left to right
> 
> 
>        The following notes provide further information to the above table:
> 
> 
>        [1] The  ++  and -- operators at this precedence level are the postfix
>            flavors of the operators.
>        [2] The ++ and -- operators at this precedence level  are  the  prefix
>            flavors of the operators.
> 
> 
> Linux man-pages 6.03              2023-02-05                      operator(7)
> 
> 
> ```
> 
> ----- End forwarded message -----
> 
> -- 
> <https://www.alejandro-colomar.es/>
> Looking for a remote C programming job at the moment.



-- 
<https://www.alejandro-colomar.es/>
Looking for a remote C programming job at the moment.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

      reply	other threads:[~2024-01-19 22:58 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <tencent_98F329827330C28C2727E9F2106379868609@qq.com>
2024-01-19 21:50 ` FWD: lijh8@qq.com: about operator(7) precedence Alejandro Colomar
2024-01-19 22:58   ` Alejandro Colomar [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Zar-liK0Ev692sTQ@debian \
    --to=alx@kernel.org \
    --cc=lijh8@qq.com \
    --cc=linux-man@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).