Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Remembering C's operator precedence rules Date: Sat, 09 Mar 2019 12:32:51 -0800 Organization: None to speak of Lines: 42 Message-ID: References: <87lg1o9efx.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="27fa8bf6c916b83812acd4d11f7fc6c5"; logging-data="21266"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/fOPTze91j+avgNWcb4MzG" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:3SnbKNnFQ33DEYGeDHPniWCgA+w= sha1:xgMweR4JW1lXEZbQRYso54f+ol4= Xref: csiph.com comp.lang.c:141846 Ben Bacarisse writes: [...] > Within the big block of relational operators, another grouping that > looks odd at first is that == and != have their own precedence level > just below <, >, <= and >=. This is another case that it's probably > wise to assume not everyone knows, but /you/ might remember it if you > consider that == and != make sensible logical connectives, rather like > && and ||. For example, to test if four variables are in order -- > either ascending or descending -- you can write > > a <= b == b <= c == c <= d > > Compare that with > > a <= b && b <= c && c <= d > > for a single order. I think you forgot about associativity. x == y == z means (x == y) == z not (x == y) && (y == z) (Some languages do chain comparison operators, and some forbid expressions that would be ambiguous. C just sits back and lets you shoot yourself in the foot.) [...] > So how do you either remember these, or work them out from first > principles? Do you have any handy mnemonics or idiomatic examples to > share? I usually have a copy of n1570.pdf open. When necessary, look at the table of contents and check the subsection names in section 6.5. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Will write code for food. void Void(void) { Void(); } /* The recursive call of the void */