DEFINITE bug in Turbo C 2.0

D. K. Smith dks at shumv1.uucp
Fri Mar 23 17:45:26 AEST 1990


In article <802 at zeusa.UUCP> hendrik at zeusa.UUCP (Hendrik Vermooten) writes:
>Here is a definite bug in C (discovered by Renier v. Wyk)
>
>#include <stdio.h>
>#define SQR(a) a*a
>main ()
>{
>	int a = 10, b = 10, x, y;
>
>	x = SQR (++a);
>	y = SQR (b++);
>	printf ("%d\n", x);
>	printf ("%d\n", y);
>	a = 10;
>	b = 10;
>	printf ("%d\n", SQR (++a));
>	printf ("%d\n", SQR (b++));
>}
>

Sorry about duplicating part of my previous post but I decided 
to compare the results of the above code on my THINK C 3.02 compiler
with the same code however using doubles (which in THINK C utilizes
the Motorola 68881 math coproc).

Integers yielded:
144
110
144
110


Using the following lines of code for replacements to the above lines...

double a = 10, b = 10, x, y;

fprintf (dfp, "%e\n", x);
fprintf (dfp, "%e\n", y);
fprintf (dfp, "%e\n", SQR (++a));
fprintf (dfp, "%e\n", SQR (b++));

This resulted in...
1.320000e+002
1.100000e+002
1.320000e+002
1.100000e+002


This is interesting but I cannot imagine the intent of the code
that led to this discovery.  What is a compiler to do?  That pre-incrmt
is a real nasty!


dk smith



More information about the Comp.lang.c mailing list