context-independent macros

Radford Neal radford at calgary.UUCP
Tue Feb 5 10:03:05 AEST 1985


> Instead of:
> 	# define macro(args)\
> 		if (1) {\
> 			/* macro body */\
> 		}\
> 		else
> 
> How about:
> 	#define	macro(args)\
> 		(Statement1 ,\
> 		Statement2 ,\
> 		...
> 		Statement3)
> 
> This should work correctly...

It doesn't work correctly. The statements might not be expressions (e.g. they
could be loops).

I posted a flame months ago on the deficiencies of the preprocessor. I
advocated an "inline" attribute for procedures to allow one to avoid its use.
It is quite impossible to write a macro to duplicate the following (for 
example):

   int f(a)
     char *a;
   { int c;
     c = 0;
     while (*a) { putchar(*a); c += 1; a += 1; }
     return c;
   }

The example above, however, can be done repressing only moderate amounts of
disgust via:

   #define macro(args) do { statement1; statement2; ... ; } while (0)

Note no semicolon at the end.

     Radford Neal
     The University of Calgary



More information about the Comp.lang.c mailing list