Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site brl-tgr.ARPA Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!seismo!brl-tgr!tgr!cottrell@nbs-vms.ARPA From: cottrell@nbs-vms.ARPA (COTTRELL, JAMES) Newsgroups: net.lang.c Subject: ALL compilers? Message-ID: <1896@brl-tgr.ARPA> Date: Thu, 3-Oct-85 18:58:51 EDT Article-I.D.: brl-tgr.1896 Posted: Thu Oct 3 18:58:51 1985 Date-Received: Sat, 5-Oct-85 07:29:36 EDT Sender: news@brl-tgr.ARPA Lines: 40 > > ...even trivial compilers will generate identical code for > > if(p) /* BAD */ > > if(p != (anytype *)0) /* GOOD */ > > ALL C COMPILERS GENERATE IDENTICAL CODE FOR THOSE TWO CASES. Anything that > does *not* generate identical code for them - even if that means comparing > the bits stored in "p" with the bit pattern of 0x555555 - is not a C > compiler. It is a compiler for a language which resembles C in some ways > but isn't C. > Guy Harris This is probably quibbling. It all depends on the degree of optimization in the compiler. Lets take the two statements (on a PDP-11): `if (p)' vs `if (p != 0)' asm: tst p cmp p,#0 beq false beq false Now a smart optimizer knows how to optimize the compare with zero into a test, but a simple, literal-minded do-what-I-say not what-I-mean one semester three credit course compiler might not optimize. It get hairier with something like: `if (a = b)' vs `if ((a = b) != 0)' asm: mov a,b mov a,b cmp a,#0 (or tst a) beq false beq false Here we can optimize away the compare instruxion entirely. The point is that both would be C compilers, one would just be better. Guy is right, however, in that programmers should not get hooked on details like this. Both are *conceptually* equivalent & I would bet that all *real* C compilers do optimize them to *be* equivalent. I used to worry about multiplying & dividing by powers of two & wrote them as shifts until I saw that the optimizer did that for me. jim cottrell@nbs */ ------