Is #define THING -10 completely safe?

Chris Torek torek at elf.ee.lbl.gov
Thu Feb 14 18:43:04 AEST 1991


In article <1702 at svin02.info.win.tue.nl> debra at svin02.info.win.tue.nl
(Paul de Bra) writes:
>   if INT_MAX is 2147483647 then INT_MIN should not be written as
>   -2147483648 but as (-2147483647-1)

Paul is correct here.  The type and value of -2147483648 are unsigned long
and 2147483648 respectively (on a typical two's complement 32-bit machine).
The constant is made up of two subexpressions, namely unary minus and
the integral constant `2147483648', and the latter is an unsigned long.
Negation does not alter the type, and in this particular case it leaves
the value unchanged as well.

>-2147483648 is a (constant) expression, not evaluated by the preprocessor
>but by the compiler.

Actually, it is at times evaulated by both.  The preprocessor has
arithmetic that is similar to, but not the same as, that in the
compiler.  When I discovered this I raised a very minor fuss (a
fusslet? fusslette? :-) ) since it complicates the preprocessor, which
must understand unsigned values (including U-suffixed constants):

	#include <stdio.h>

	void a() {
	#if -1 > 1
		printf("bad\n");
	#else
		printf("good\n");
	#endif
	}

	void b() { 
	#if -1U > 1
		printf("good\n");
	#else
		printf("bad\n");
	#endif
	}

	int main() { a(); b(); return 0; }

must print `good' twice.
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab EE div (+1 415 486 5427)
Berkeley, CA		Domain:	torek at ee.lbl.gov



More information about the Comp.lang.c mailing list