Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!mailrus!ncar!mephisto!ncsuvx!news From: dks@shumv1.uucp (D. K. Smith) Newsgroups: comp.lang.c Subject: Re: DEFINITE bug in Turbo C 2.0 Message-ID: <1990Mar23.074526.8286@ncsuvx.ncsu.edu> Date: 23 Mar 90 07:45:26 GMT References: <802@zeusa.UUCP> Sender: news@ncsuvx.ncsu.edu (USENET News System) Reply-To: dks@shumv1.ncsu.edu (D. K. Smith) Organization: NCSU Computing Center Lines: 54 In article <802@zeusa.UUCP> hendrik@zeusa.UUCP (Hendrik Vermooten) writes: >Here is a definite bug in C (discovered by Renier v. Wyk) > >#include >#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