Deutsch English Français Italiano |
<rules-20241003203050@ram.dialup.fu-berlin.de> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!fu-berlin.de!uni-berlin.de!not-for-mail From: ram@zedat.fu-berlin.de (Stefan Ram) Newsgroups: alt.folklore.computers,comp.os.linux.misc Subject: C operator precedence rules Date: 3 Oct 2024 19:37:07 GMT Organization: Stefan Ram Lines: 248 Expires: 1 Jul 2025 11:59:58 GMT Message-ID: <rules-20241003203050@ram.dialup.fu-berlin.de> References: <pan$96411$d204da43$cc34bb91$1fe98651@linux.rocks> <5mqdnZuGq4lgwm_7nZ2dnZfqnPSdnZ2d@earthlink.com> <vcub5c$36h63$1@dont-email.me> <1r0e6u9.1tubjrt1kapeluN%snipeco.2@gmail.com> <vcuib9$37rge$5@dont-email.me> <vcvuhh$3hroa$2@dont-email.me> <llhieuF8ej2U2@mid.individual.net> <20240925083451.00003205@gmail.com> <Pascal-20240925164718@ram.dialup.fu-berlin.de> <mdd4j63pmo1.fsf_-_@panix5.panix.com> <oJ-cnQSrLZDYdGX7nZ2dnZfqnPWdnZ2d@earthlink.com> <vdatb6$1l4ch$8@dont-email.me> <vdauah$1lq1u$1@dont-email.me> <lltt6uF4fseU4@mid.individual.net> <vdcn1q$1tmdr$5@dont-email.me> <vddqoe$264fi$2@dont-email.me> <vdf4le$2cn51$6@dont-email.me> <vdh2l1$2p7e2$1@dont-email.me> <vdh3ku$2path$1@dont-email.me> <vdhqnf$2t1fi$4@dont-email.me> <e6tLO.276711$FzW1.154698@fx14.iad> <lm86g5Flrc3U11@mid.individual.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de nM8H30ayyUUY+8HtjBYskQbvzUQWJFQZKyFrfPZKWkOflL Cancel-Lock: sha1:lLWtJzVVNPBEv9JNDmAbvLpH51k= sha256:JRVjPfjyapJKZvzD3yfjDCo91uJ6bC8yqixo96eP70o= X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved. Distribution through any means other than regular usenet channels is forbidden. It is forbidden to publish this article in the Web, to change URIs of this article into links, and to transfer the body without this notice, but quotations of parts in other Usenet posts are allowed. X-No-Archive: Yes Archive: no X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some services to mirror the article in the web. But the article may be kept on a Usenet archive server with only NNTP access. X-No-Html: yes Content-Language: en-US Bytes: 9562 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 ========== REMAINDER OF ARTICLE TRUNCATED ==========