C operator precedence rules

Liste des GroupesRevenir à ol misc 
Sujet : C operator precedence rules
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : alt.folklore.computers comp.os.linux.misc
Date : 03. Oct 2024, 21:37:07
Autres entêtes
Organisation : Stefan Ram
Message-ID : <rules-20241003203050@ram.dialup.fu-berlin.de>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
rbowman <bowman@montana.com> wrote or quoted:
On Thu, 3 Oct 2024 03:04:11 -0600, Louis Krupp wrote:
Maybe add parentheses around the comparison, especially if it's long or
complex, which this isn't:
I tend to use parens with operators if it isn't a simple expression to
make the precedence explicit.

  Could you play this sonata for us?

  Pianist plays the Beethoven Sonata from memory.
  (He also knows some other pieces by heart.)

  Could you translate this sentence for us?

  Translator translates the sentence, after having learned
  more than 10000 words, many of which have several forms
  and meanings, idioms, and complex rules of grammar and
  usage.

  Could you explain "a & b << c + 1" for us?

  Look Sir, I'm a professional C programmer, doing this eight
  hours a day, 40 hours a week, for many years. But C has
  about 17 levels of precedence! Memorizing all of them is
  simply beyond human capabilities! So please give me some parens.
  I would say, otherwise, you would be to blame for having
  written such a cluttered mess!

                             ~~~

  C is a language with some subtleties, and programming often
  is somewhat complex overall. If you don't trust a C programmer
  to even get an expression as simple as "a & b << c + 1",
  how is he supposed to get more subtle things right?

                             ~~~

  We start with -2 * 3 + 4.

  We do not have to memorize precedence here, because we know
  it from school. So, we have

prefix           - ! * ...
multiplicative   * /
additive         + -

  . To get the full list of the "calculations", as I call
  these operations, we just need to add one layer "postfix"
  above and one layer "shift" below:

postfix          [] () ...
prefix           - ! * ...
multiplicative   * /
additive         + -
shift            << >>

  . One can remember the position of those layers by thinking
  about the meaning one wants to have to expressions like -a[ 3 ]
  and 1 << 2 + 3. The amount of a shift is often expressed
  with arithmetic expressions, so we want no parens there.

  Now we'll go beyond what I called "calculations":

  The results of calculations usually are compared. We rarely
  want ( 2 < 3 )+ 1, but more often 2 <( 3 + 1 ).

postfix          [] () ...
prefix           - ! * ...
multiplicative   * /
additive         + -
shift            << >>
comparisons      < > <= >=

  Though both are quite rare, (x>0)==(y>0) still should be
  needed more often than (x==0)>(y==0).

postfix          [] () ...
prefix           - ! * ...
multiplicative   * /
additive         + -
shift            << >>
comparisons      < > <= >=
equalities       == !=

  When shifting, in the end, we often mask using "&". So we
  want to be able to write "word & 1 << 3" without parens.

postfix          [] () ...
prefix           - ! * ...
multiplicative   * /
additive         + -
shift            << >>
comparisons      < > <= >=
equalities       == !=
bit and          &

  Sometimes, one might want to write x==0&y==0 without
  shortcut evaluation (to avoid a jump-operation), so "&"
  has less precedence than "==".

  The "and" operation is very similar to multiplication:
  0*0=0, 0*1=0, 1*0=0, 1*1=1. The "or" operation is very
  similar to addition: 0+0=0, 0+1=1, 1+0=1, 1+1=2.
  So, "and" is "multiplicative" and "or" is "additive".
  Additive operations have a smaller precedene than
  multiplicative operations.

postfix          [] () ...
prefix           - ! * ...
multiplicative   * /
additive         + -
shift            << >>
comparisons      < > <= >=
equalities       == !=
bit and          &
bit or           |

  The bitwise exclusive or is neither clearly additive nor
  clearly multiplicative, so it's middle ground, so to speak.

postfix          [] () ...
prefix           - ! * ...
multiplicative   * /
additive         + -
shift            << >>
comparisons      < > <= >=
equalities       == !=
bit and          &
bit exclusive or ^
bit or           |

  The logical operations are "high level" operations, since
  one might want to require to bits to be set via x&3 && y&4.

postfix          [] () ...
prefix           - ! * ...
multiplicative   * /
additive         + -
shift            << >>
comparisons      < > <= >=
equalities       == !=
bit and          &
bit exclusive or ^
bit or           |
logical and      &&
logical or       ||

  The "if" expression is as high level as a statement. One wants
  to be able to write: x > 2 && y > 3 ? ... .

postfix          [] () ...
prefix           - ! * ...
multiplicative   * /
additive         + -
shift            << >>
comparisons      < > <= >=
equalities       == !=
bit and          &
bit exclusive or ^
bit or           |
logical and      &&
logical or       ||
if               ?:

  (Possibly not all details of the grammar of the ternary
  operator can be specified using the concept of precedence,
  and so one might need to refer to the actual grammar.)

  And whatever expression you write, you want to be able to
  write it on the right-hand side of an assignment operator
  without parantheses.

postfix          [] () ...
prefix           - ! * ...
multiplicative   * /
additive         + -
shift            << >>
comparisons      < > <= >=
equalities       == !=
bit and          &
bit exclusive or ^
bit or           |
logical and      &&
logical or       ||
if               ?:
assignment       =

  One exception to this is when you want to write a
  sequence of assignments, such as: a=2,b=3.

postfix          [] () ...
prefix           - ! * ...
multiplicative   * /
additive         + -
shift            << >>
comparisons      < > <= >=
equalities       == !=
bit and          &
bit exclusive or ^
bit or           |
logical and      &&
logical or       ||
if               ?:
assignment       =
comma            ,

  It might help to add some structure:

calculations postfix          [] () ...
             prefix           - ! * ...
             multiplicative   * /
             additive         + -
             shift            << >>

comp./eq.    comparisons      < > <= >=
             equalities       == !=

bit related  bit and          &
             bit exclusive or ^
             bit or           |

logical      logical and      &&
             logical or       ||
             if               ?:

assignment   assignment       =
comma        comma            ,

  So one could start out learning the coarse first column
  with only six levels by heart, and the "details" from the
  seconds column later.

  One also can observe that the operators from [] to | are
  used to "obtain" values, while the operators from ?: to
  , are used to "process" values, and "&&" and "||" are in
  between.

  (I wrote all of the above from memory, but looked at a
  precedence table yesterday.)

  No way, José! Those were actually all snippets from my old posts
  about wrapping your head around operator precedence. Talk about
  a blast from the past! I've been beating that drum for ages.
  It's like trying to explain the ins and outs of In-N-Out's secret
  menu - takes practice, but once you get it, you're golden.
  Just got to keep plugging away until it clicks, you know?



Date Sujet#  Auteur
21 Sep 24 * Can't Avoid That Shit Rust - Even On Gentoo567Farley Flud
21 Sep 24 +- Re: Can't Avoid That Shit Rust - Even On Gentoo1Farley Flud
24 Sep 24 `* Re: Can't Avoid That Shit Rust - Even On Gentoo565186282@ud0s4.net
24 Sep 24  +* Re: Can't Avoid That Shit Rust - Even On Gentoo6Lawrence D'Oliveiro
24 Sep 24  i+* Re: Can't Avoid That Shit Rust - Even On Gentoo2Pancho
25 Sep 24  ii`- Re: Can't Avoid That Shit Rust - Even On Gentoo1186282@ud0s4.net
25 Sep 24  i`* Re: Can't Avoid That Shit Rust - Even On Gentoo3186282@ud0s4.net
25 Sep 24  i +- Re: Can't Avoid That Shit Rust - Even On Gentoo1Lawrence D'Oliveiro
25 Sep 24  i `- Re: Can't Avoid That Shit Rust - Even On Gentoo1rbowman
24 Sep 24  +* Re: Can't Avoid That Shit Rust - Even On Gentoo4D
25 Sep 24  i`* Re: Can't Avoid That Shit Rust - Even On Gentoo3186282@ud0s4.net
25 Sep 24  i `* Re: Can't Avoid That Shit Rust - Even On Gentoo2Lawrence D'Oliveiro
28 Sep 24  i  `- Re: Can't Avoid That Shit Rust - Even On Gentoo1186282@ud0s4.net
24 Sep 24  +* The joy of FORTRAN508Lars Poulsen
24 Sep 24  i+* Re: The joy of FORTRAN256Sn!pe
24 Sep 24  ii+* Re: The joy of FORTRAN160The Natural Philosopher
24 Sep 24  iii+* Re: The joy of FORTRAN34Lawrence D'Oliveiro
25 Sep 24  iiii+* Re: The joy of FORTRAN22rbowman
25 Sep 24  iiiii+* Re: The joy of FORTRAN19Bob Eager
25 Sep 24  iiiiii+* Re: The joy of FORTRAN16rbowman
25 Sep 24  iiiiiii+* Re: The joy of FORTRAN13Bob Eager
27 Sep 24  iiiiiiii`* Re: The joy of FORTRAN12Peter Flass
27 Sep 24  iiiiiiii +- Re: The joy of FORTRAN1Niklas Karlsson
27 Sep 24  iiiiiiii +* Re: The joy of FORTRAN6R Daneel Olivaw
28 Sep 24  iiiiiiii i+* Re: The joy of FORTRAN3Gordon Henderson
28 Sep 24  iiiiiiii ii+- Re: The joy of FORTRAN1Peter Flass
28 Sep 24  iiiiiiii ii`- Re: The joy of FORTRAN1Bob Eager
28 Sep 24  iiiiiiii i+- Re: The joy of FORTRAN1Peter Flass
28 Sep 24  iiiiiiii i`- Re: The joy of FORTRAN1Lars Poulsen
27 Sep 24  iiiiiiii +- Re: The joy of FORTRAN1Bob Eager
28 Sep 24  iiiiiiii `* Re: The joy of FORTRAN3Lawrence D'Oliveiro
28 Sep 24  iiiiiiii  +- Re: The joy of FORTRAN1Peter Flass
30 Sep22:26  iiiiiiii  `- Re: The joy of FORTRAN1Rich Alderson
25 Sep 24  iiiiiii`* Re: The joy of FORTRAN2Lynn Wheeler
25 Sep 24  iiiiiii `- Re: The joy of FORTRAN1Kerr-Mudd, John
25 Sep 24  iiiiii`* Re: The joy of FORTRAN2Chris Ahlstrom
25 Sep 24  iiiiii `- Re: The joy of FORTRAN1Bob Eager
27 Sep 24  iiiii`* Re: The joy of FORTRAN2Peter Flass
27 Sep 24  iiiii `- Re: The joy of FORTRAN1moi
25 Sep 24  iiii+* Re: The joy of FORTRAN2Lynn Wheeler
25 Sep 24  iiiii`- Re: The joy of FORTRAN1Lawrence D'Oliveiro
25 Sep 24  iiii+* Re: The joy of FORTRAN2Chris Ahlstrom
27 Sep 24  iiiii`- Re: The joy of FORTRAN1Peter Flass
25 Sep 24  iiii+- Re: The joy of FORTRAN1Chris Ahlstrom
25 Sep 24  iiii+- Re: The joy of FORTRAN1Kerr-Mudd, John
27 Sep 24  iiii+* Re: The joy of FORTRAN2Peter Flass
27 Sep 24  iiiii`- Re: The joy of FORTRAN1Dennis Boone
27 Sep 24  iiii`* Re: The joy of FORTRAN3Andy Walker
28 Sep 24  iiii +- Re: The joy of FORTRAN1Lawrence D'Oliveiro
28 Sep 24  iiii `- Re: The joy of FORTRAN1Peter Flass
25 Sep 24  iii+* Re: The joy of FORTRAN2Peter Flass
25 Sep 24  iiii`- Re: The joy of FORTRAN1Lawrence D'Oliveiro
25 Sep 24  iii+- Re: The joy of FORTRAN1Peter Flass
25 Sep 24  iii+- Re: The joy of FORTRAN1Peter Flass
25 Sep 24  iii+- Re: The joy of FORTRAN1rbowman
25 Sep 24  iii+* Re: The joy of FORTRAN116Woozy Song
25 Sep 24  iiii`* Re: The joy of FORTRAN115rbowman
25 Sep 24  iiii +* Re: The joy of FORTRAN3Lawrence D'Oliveiro
27 Sep 24  iiii i`* Re: The joy of FORTRAN2Peter Flass
30 Sep12:58  iiii i `- Re: The joy of FORTRAN1Waldek Hebisch
25 Sep 24  iiii +* Re: The joy of FORTRAN37Chris Ahlstrom
25 Sep 24  iiii i+* Re: The joy of FORTRAN3R Daneel Olivaw
25 Sep 24  iiii ii`* Re: The joy of FORTRAN2Kerr-Mudd, John
25 Sep 24  iiii ii `- Re: The joy of FORTRAN1R Daneel Olivaw
25 Sep 24  iiii i+* Re: The joy of FORTRAN6Lynn Wheeler
26 Sep 24  iiii ii+- Re: The joy of FORTRAN1Pancho
26 Sep 24  iiii ii`* Re: The joy of FORTRAN4Lynn Wheeler
26 Sep 24  iiii ii `* Re: The joy of FORTRAN3Lawrence D'Oliveiro
27 Sep 24  iiii ii  `* Re: The joy of FORTRAN2rbowman
27 Sep 24  iiii ii   `- Re: The joy of FORTRAN1Lawrence D'Oliveiro
26 Sep 24  iiii i+* Re: The joy of FORTRAN3rbowman
26 Sep 24  iiii ii+- Re: The joy of FORTRAN1John Ames
26 Sep 24  iiii ii`- Re: The joy of FORTRAN1Lawrence D'Oliveiro
26 Sep 24  iiii i+* Re: The joy of FORTRAN19Pancho
26 Sep 24  iiii ii+* Re: The joy of FORTRAN16Lawrence D'Oliveiro
26 Sep 24  iiii iii`* Re: The joy of FORTRAN15Pancho
26 Sep 24  iiii iii `* Re: The joy of FORTRAN14Lawrence D'Oliveiro
27 Sep 24  iiii iii  `* Re: The joy of FORTRAN13Lars Poulsen
27 Sep 24  iiii iii   +* Re: The joy of FORTRAN7Lawrence D'Oliveiro
27 Sep 24  iiii iii   i`* Re: The joy of FORTRAN6Pancho
28 Sep 24  iiii iii   i `* Re: The joy of VAX5Lawrence D'Oliveiro
28 Sep 24  iiii iii   i  `* Re: The joy of VAX4Pancho
29 Sep09:35  iiii iii   i   `* Re: The joy of VAX3Robert Marshall
29 Sep09:56  iiii iii   i    +- Re: The joy of VAX1Bob Eager
29 Sep22:15  iiii iii   i    `- Re: The joy of VAX1Peter Flass
28 Sep 24  iiii iii   `* The joy of VAX C5Lars Poulsen
28 Sep 24  iiii iii    +- Re: The joy of VAX C1The Natural Philosopher
29 Sep 24  iiii iii    +- Re: The joy of VAX C1rbowman
29 Sep22:15  iiii iii    `* Re: The joy of VAX C2Peter Flass
30 Sep01:10  iiii iii     `- Re: The joy of VAX C1Lawrence D'Oliveiro
27 Sep 24  iiii ii`* Re: The joy of FORTRAN2Peter Flass
27 Sep 24  iiii ii `- Re: The joy of FORTRAN1Lawrence D'Oliveiro
27 Sep 24  iiii i`* Re: The joy of FORTRAN5Peter Flass
27 Sep 24  iiii i +* Re: The joy of FORTRAN2Bob Eager
28 Sep 24  iiii i i`- Re: The joy of FORTRAN1Peter Flass
27 Sep 24  iiii i `* Re: The joy of FORTRAN2Lynn Wheeler
27 Sep 24  iiii i  `- Re: The joy of FORTRAN1Lynn Wheeler
25 Sep 24  iiii `* Re: The joy of FORTRAN74John Ames
25 Sep 24  iiii  +* Re: The joy of FORTRAN69Stefan Ram
25 Sep 24  iiii  i+* Re: The joy of FORTRAN2John Ames
25 Sep 24  iiii  ii`- Re: The joy of FORTRAN1Lawrence D'Oliveiro
25 Sep 24  iiii  i+* TeX and Pascal [was Re: The joy of FORTRAN]65Rich Alderson
25 Sep 24  iiii  i`- Re: The joy of FORTRAN1Lawrence D'Oliveiro
25 Sep 24  iiii  `* Re: The joy of FORTRAN4Lawrence D'Oliveiro
25 Sep 24  iii`* Re: The joy of FORTRAN4John Levine
24 Sep 24  ii+* Re: The joy of FORTRAN91rbowman
24 Sep 24  ii+* Re: The joy of FORTRAN2Lawrence D'Oliveiro
25 Sep 24  ii`* Re: The joy of FORTRAN2Bob Eager
24 Sep 24  i+* Re: The joy of FORTRAN5Bob Eager
24 Sep 24  i+- Re: The joy of FORTRAN1R Daneel Olivaw
25 Sep 24  i+- Re: The joy of FORTRAN1Peter Flass
25 Sep 24  i`* Re: The joy of FORTRAN244186282@ud0s4.net
24 Sep 24  +* Re: Can't Avoid That Shit Rust - Even On Gentoo3rek2 hispagatos
24 Sep 24  +* Re: Can't Avoid That Shit Rust - Even On Gentoo2rbowman
24 Sep 24  +- Re: Can't Avoid That Shit Rust - Even On Gentoo1The Natural Philosopher
25 Sep 24  `* Re: Can't Avoid That Shit Rust - Even On Gentoo40rbowman

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal