Is this a bug in Turbo C 2.0?

William J. Murphy murphy at pur-phy
Wed Mar 7 01:28:21 AEST 1990


Yesterday, I was debugging a program of mine and came across an interesting
difference between GNU C and Turbo C. Here is the buggy code:

long SUM;
short data1, data2, data3, data4, data5;

SUM = data1 + data2 + data3 + data4 + data5;

Here is the problem, when the sum of data? < 32767 it sums correctly on both
GNU C and TC.  Hwen the sum of data? > 32767, TC does the rollover and 
becomes negative while GNU C carries the sum as a long and returns the
correct answer.  Which one is correct? (according to ANSI standards)
Of course the fix is simple, but the rest of the program had problems
that are not so simple to fix under MSDOS.
 
SUM = (long)data1 + (long)data2 + (long)data3 + (long)data4 + (long)data5;

solved the problem.

-- 
 Bill Murphy                          murphy at newton.physics.purdue.edu
Enjoying my Amiga 2000, but holding out for a real computer: The Amiga 3000!!



More information about the Comp.lang.c mailing list