C-Debug package ... (a la "context-independent macros")

Arnold Robbins arnold at gatech.UUCP
Sat Feb 2 03:12:18 AEST 1985


Okay, folks, we've discussed this one before.  The macro

#define assert(x) if (!(x)) die_miserably("x bombed\n")

doesn't work because of problems with a missing else.  It was suggested

#define assert(x) if (!(x)) die_miserably("x bombed\n") else /* no ; */

but if the real semicolon is left off by accident, the next statement is eaten.
Now, putting braces around it also won't work:

#define assert(x) { if (!(x)) die_miserably("x bombed\n"); }

because of this:

	if (something)
		assert (something_else);/* <--- this ; will bomb cc */
	else
		another_thing_altogether();
	
So, as has been mentioned before, the two ways to do this are

#define assert(x)  do { if (!(x)) die_miserably("x bombed\n"); } while (0)

which will get executed exactly once, and protects the macro syntactically.
This also should not pose a code generation/efficiency problem.  The other
way is this:

#define assert(x)	if(x) ; else die_miserably ("x bombed\n")

which will also work. This topic has gone around (several times) before.
I am hoping that this note will settle it, so that we can discuss something
else.  Thanks,
-- 
Arnold Robbins
CSNET:	arnold at gatech	ARPA:	arnold%gatech.csnet at csnet-relay.arpa
UUCP:	{ akgua, allegra, hplabs, ihnp4, seismo, ut-sally }!gatech!arnold

Help advance the state of Computer Science: Nuke a PR1ME today!



More information about the Comp.lang.c mailing list