Nesting of a preprocessor directive within a macro defn

ravim ravim at gtenmc.UUCP
Sat Aug 25 09:32:17 AEST 1990


Is nesting of a preprocessor directive within a macro definition (RHS) not
allowed?

The following piece of code causes an error at the time of compilation.

#define RET_NULL(str)		return( \
#ifdef DEBUG
				(void) printf("DEBUG: %s\n", str), \
#endif
				(char *) 0)


Upon running preprocessor, it dawned upon me that the compiler expanded
RET_NULL macro as 'return( #ifdef DEBUG', and then gave syntax error at
the next line, that is, '(void) printf ...'.

The only way I could it make work properly is as follows:

#ifdef DEBUG
#define RET_NULL(str)		return( \
				(void) printf("DEBUG: %s\n", str), \
				(char *) 0)
#else
#define RET_NULL(str)		return((char *) 0)
#endif


The compiler I used is on Vax System V.3 (8810).

Do any of the other compilers provide embedding a preprocessor directive
within a macro definition?

Is this allowed in standard C (old style or ANSI-style) syntax?

I would be glad to know if someone could point out a better solution.

Thanks.
		-  Ravi

		(ravim at gtenmc.UUCP  ||  ravim at gtenmc.gtetele.com)



More information about the Comp.lang.c mailing list