Is #define THING -10 completely safe?

Colin Plumb ccplumb at rose.uwaterloo.ca
Mon Jan 28 10:31:42 AEST 1991


rtm at island.COM (Richard Minner) wrote:
>> #define INT_MIN     -2147483648  /* min decimal value of an "int" */
>
> Something I've been curious about, should the above be
> #define INT_MIN   (-2147483648)
>
> The precedence rules seem to imply that ()'s aren't needed
> with a negative integer constant.  Could someone confirm or
> deny this please?  (I always use ()'s out of habit.)

Well, unary - has higher precedence than anything except the postfix
opertors:
[expression]
(argument-list)
.identifier
->identifier
++
--

Now, none of these are applicable to intergers, so it's safe... except!
As all loyal followers of the Obfuscated C code contest know,
array[i] == *(array+i) == *(i+array) == i[array].  So

#define X1 -10
#define X2 (-10)
int i, foo[50];
int *p = &foo[25];

for (i = 0; i < 50; i++)
	foo[i] = i;

printf("%d, %d\n", X1[p], X2[p]);

Will print "-35, 15".

If onbody's used this particular perversion in the obfuscated C code contest
yet, I'm sure they will soon.
-- 
	-Colin



More information about the Comp.lang.c mailing list