When do you use "if ( a = b )"? (was Re: Funny mistake)

Blair P. Houghton bhoughto at hopi.intel.com
Wed Mar 20 10:42:08 AEST 1991


In article <1991Mar19.192416.13756 at unlv.edu> grover at lowell.cs.unlv.edu (Kevin Grover) writes:
>michi at ptcburp.ptcbu.oz.au (Michael Henning) writes:
>>grover at sonny-boy.cs.unlv.edu (Kevin Grover) writes:
>>>
>>>	#define EQU ==
>>>	if ( a EQU b)
>>
>>NO, NO, NO ! Please don't do this.
>>And besides, are you going to change every occurrence of == to EQU in every
>
>[...]I do not understand why you are so against such a
>use of define.  I agree that it might be a pain on a
>continual basis, but any person who expects to use C
>regularly will (hopefully) discover this, and finally
>learns that using = in a test is very usefull, or possibly
>quite using C. [sic]

#define should _never_ be used to replace existing tokens.

Doing so constitutes a very effective obfuscation.  Check
the 1990 winners of the International Obfuscated C Code
Contest; you won't find a single one that doesn't use
#define to thoroughly encrypt the code).

#define has many uses in the vein of anti-obfuscation.
E.g.:  creating lexical abbreviations for complex
inventions; localizing the definition of a widely-used
constant; and, giving mnemonic significance to an otherwise
anonymous token (like, `#define BOHR_MAGNETON (9.27e-24)').

What "any person who expects to use C learns" is that `='
and `==' are (very) likely to be interchanged, and it's
wise to make a pass through your code just to look for
those spots, when debugging indicates that bad values may
be causing improper flow of control.

				--Blair
				  "But what else is a debugger for?"

P.S.  Emacs obviates the desire to `#define BEGIN {' and
`#define END }', by automatically indicating matching
braces (although your style should do that itself), but
has anyone made it check or warn that you're
using `=' and `==' in an uncommon way?



More information about the Comp.lang.c mailing list