Obscure C code (was Re: Excessive optimization)

Chuck Guzis cclist at sydex.com
Thu Dec 2 22:35:47 CST 2010


On 2 Dec 2010 at 19:53, Brent Hilpert wrote:

> The notion of integer divide is pretty well defined. I'd be pretty
> annoyed if I specced two ints, did a divide and didn't get back the
> 'standard' result. I don't have a C ref at hand, but in C at least, I
> think it would be stretching acceptable claims of language flexibility
>  or non-specification for compiler implementors to be taking leniency
> here.

Oh, I agree, but K&R is silent on exceptions.

Does any C detect integer overflow (i.e. subtracting a large positive 
number from a negative and getting a positive number back)?  While 
some hardware can trap this behavior, there's a lot that can't.  
There may be a status flag set somewhere, but testing takes cycles 
and is generally not performed.

Source:

main()
{

/*   program demonstrating integer subtraction */

  int a, b, c;

  a = -31000;
  b = 12345;
  c = a - b;
  printf( "%d minus %d is %d\n", a, b, c);
}

Compile and run:


D:\tmp>cl /AS x.c
Microsoft (R) C/C++ Optimizing Compiler Version 8.00c
Copyright (c) Microsoft Corp 1984-1993. All rights reserved.

x.c

Microsoft (R) Segmented Executable Linker  Version 5.60.339 Dec  5 
1994
Copyright (C) Microsoft Corp 1984-1993.  All rights reserved.

Object Modules [.obj]: x.obj
Run File [x.exe]: "x.exe" /noi
List File [nul.map]: NUL
Libraries [.lib]:
Definitions File [nul.def]: ;

D:\tmp>x
-31000 minus 12345 is 22191

--Chuck




More information about the cctalk mailing list