Path: utzoo!attcan!utgpu!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!bloom-beacon!adam.pika.mit.edu!scs From: scs@adam.pika.mit.edu (Steve Summit) Newsgroups: comp.lang.c Subject: Re: Compiler bug Keywords: constant expressions Message-ID: <8511@bloom-beacon.MIT.EDU> Date: 18 Dec 88 06:13:49 GMT References: <4279@freja.diku.dk> <733@auspex.UUCP> Sender: daemon@bloom-beacon.MIT.EDU Reply-To: scs@adam.pika.mit.edu (Steve Summit) Lines: 71 In article <4279@freja.diku.dk> njk@freja.diku.dk (Niels Jurgen Kruse) writes: >Consider this little one liner: >-------- foo.c -------- >int foo = 1 && 0; >------ end foo.c ------ >On a Bsd 4.3 VAX 785, cc complains : >"foo.c", line 1: illegal initialization [several other failures described] >So 3 out of 4 get it wrong. This bug seems to bee contagious. In article <733@auspex.UUCP> guy@auspex.UUCP (Guy Harris) writes: >3 out of 4 are based on PCC... >The compiler problem may be due to PCC's incredible sensitivity to tiny >perturbations in the order in which various operations are done on the >expression trees... Or, more likely, it has to do with the unfortunate description of constant expressions in the original K&R (first edition, section 15, page 211): ...the expression can involve... the binary operators + - * / % & | ^ << >> == != < > <= >= or... the unary operators - ~ or... the ternary operator ? : Ritchie's original pdp11 C compiler also reports "illegal initialization" for an initialization involving &&. Harbison and Steele note (second edition, p. 188) that "the original description of C did not mention that the operator ! is allowed in constant expressions, but this obviously was an oversight or a typographical error." Their list of operators allowed in constant expressions includes && and ||, without comment. K&R, second edition (section A7.19, p. 209) contains no explicit list of operators, implying that all (other than obvious exceptions such as unary &) are now allowed. Before rejecting the historical restriction on && and || out-of-hand, recall that && and || are somewhat different from the other binary operators: they affect control flow, in that evaluation of the right-hand-side is short-circuited if its value will not affect the result. This fact does not justify their being disallowed in constant expressions, but it makes it easier to understand why an implementation might (perhaps inadvertently) do so. Since "control flow" is meaningless in constant expressions, the handling of && and || in constant expressions could be different (read "nonexistent"), as opposed to normal expressions. (Of course, the interpretive handling of constant expressions is already pretty different from the code generation performed for normal expressions.) Another illustration of boolean foibles in constant expressions is something like #define a 0 #if a == 0 || 4 / a < 2 which causes some (many?) cpp's to abort with a divide by 0 fault. Steve Summit scs@adam.pika.mit.edu