leichter@yale-comix.UUCP (Jerry Leichter) (04/20/84)
Brian Jones (drutx!qwerty) presents a program to test C compilers for proper evaluation of expressions involving side-effects and coercions. I've tried his sample program on two more C compilers, with the following results: VAX11-C (VMS) DECUS C (VMS, compatibility mode) ------------- --------------------------------- 20 2 20 2 20 3 0 3 30 5 30 5 30 6 33 6 The VAX11-C results are identical to those Brian found for "Unix on a Dec 11/70, Ver. 3.0"; he believes this is the (a?) "right answer". (I'm not questioning his judgement; I haven't looked at the code closely enough to form any sort of opinion.) The DECUS C results - which would be the same on all the operating systems and machines (VAXes and 11's) DECUS C runs under - are identical to what Brian found for "Mark Williams Co. C on an Intel 86/330 System, with 8087 support". I find this an interesting statement, since I think the same person (Dave Conroy) did most of both compilers. DECUS C required two changes to the code as distributed. The first is a known DECUS C restriction: Local variables cannot be initialized. This required that j in main be initialized in a separate statement. The second is either a bug or a change in the language definition (DECUS C pre-dates K&R): DECUS C requires a parenthesized expression after return; this requires that: return a; in ifunc() be changed to: return (a); For people who might have missed the original article and want to try this test out on their favorite compiler, here is the version I used, with the edits for DECUS C in place (might help on other old compilers): /* * Simple E1 op= E2 test */ #include <stdio.h> main() { int i, j, *ifunc(); j = 0; i = 100; *ifunc(&i, &j) = *ifunc(&i, &j) * .2; printf("%d %d\n", i, j); i = 100; *ifunc(&i, &j) *= .2; printf("%d %d\n", i, j); i = 100; *ifunc(&i, &j) = *ifunc(&i, &j) / 3.3; printf("%d %d\n", i, j); i = 100; *ifunc(&i, &j) /= 3.3; printf("%d %d\n", i, j); } int *ifunc(a, b) int *a, *b; { ++*b; return (a); } Brian's results, in summary: 1 2 3 4 5 6 ---- ---- ---- ---- ---- ---- 19 2 20 2 20 2 20 2 20 2 19 2 0 3 0 3 20 3 20 4 0 3 19 4 30 5 30 5 30 5 30 6 30 5 30 5 33 6 33 6 30 6 30 8 33 6 30 6 1) Maxi-Unix on an Ahmdahl (sp?), Ver. 5.0 2) Unix on a Vax 11/780, Ver. 5.0 3) Unix on a Dec 11/70, Ver. 3.0 **right answer** 4) Unix on a 3B20, Ver. 5.0 5) Mark Williams Co. C on an Intel 86/330 System, with 8087 support 6) Ancient Yourdon (I think) compiler on a Dec 11/44 **right answer** -- Jerry decvax!yale-comix!leichter leichter@yale
qwerty@drutx.UUCP (JonesBD) (04/23/84)
I would like to correct the results posted in column 6. It should read: 6 ----- 19 2 19 3 (was 4) 30 5 30 6 My original text was wrong. SBT (Sorry 'Bout That) Brian Jones
gwyn@brl-vgr.ARPA (Doug Gwyn ) (05/02/84)
DECUS C does not "predate K&R". The requirement that one write return (a); rather than return a; has not been in the C language since 1975 and I doubt it was there before.