[comp.lang.c] Nesting of a preprocessor directive within a macro defn

ravim@gtenmc.UUCP (ravim) (08/25/90)

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@gtenmc.UUCP  ||  ravim@gtenmc.gtetele.com)

henry@zoo.toronto.edu (Henry Spencer) (09/02/90)

In article <849@gtenmc.UUCP> ravim@gtenmc.UUCP (Ravi Kumar Mandava) writes:
>Is nesting of a preprocessor directive within a macro definition (RHS) not
>allowed?

It is not possible.  The very first thing that happens to ANSI C source
during compilation is that backslashed newlines are stripped out, so any
"#" within the RHS is no longer at the start of a line and hence does
not begin a preprocessor directive.

(And incidentally, the result of macro expansion "is not processed as a
preprocessing directive even if it resembles one", section 3.8.3.4.)

Some old C compilers may have permitted such things, due to the vagueness
of the old definition of how the preprocessor features interacted.
-- 
TCP/IP: handling tomorrow's loads today| Henry Spencer at U of Toronto Zoology
OSI: handling yesterday's loads someday|  henry@zoo.toronto.edu   utzoo!henry