C macros (was comma operator: keep away?)

Chris Torek chris at mimsy.UUCP
Wed Apr 26 13:43:52 AEST 1989


In article <1317 at ns.network.com> ddb at ns.network.com (David Dyer-Bennet) writes:
>[deleted referent] is an example of the weakness of the macro facility
>in C (one of the most light-weight macro facilities I've ever had foisted
>on me).  Without taking a position on the general value of the comma
>operator -- the correct solution to this one is to improve the macro
>facility, not provide a way to kludge some (but not all) cases.

C's macro preprocessor is intended to be weak.  Fancier macro expanders
(such as m4) are available (although their syntaxes often clash with C's,
and/or with each other's).  What is missing from C (for operations like
getchar and putchar) is not better macros, but rather inline functions.
Inline functions behave exactly like true functions, with the exception
that they may not be recursive, and (unless the compiler is clever) they
cannot contain static variables.  The multiple argument evaluation problem
vanishes; stdio.h could have putc defined as

	inline int putc(char c, FILE *fp) {
		/* if there is room, just stuff it in, unless \n */
		if (--fp->_count >= 0 &&
		    ((fp->_flags & _LINEBUFFERED) == 0 || c != '\n'))
			*fp->_ptr++ = c;
			return c;
		}
		/* no room, or line buffered newline */
		return _put_and_flush(c, fp);
	}
	#define putchar(c) putc(c, stdout) /* simpler as a macro */

[Yes, I know gcc has inline functions.]
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list