Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!ncar!tank!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: C macros (was comma operator: keep away?) Message-ID: <17109@mimsy.UUCP> Date: 26 Apr 89 03:43:52 GMT References: <19913@iuvax.cs.indiana.edu> <10092@smoke.BRL.MIL> <1317@ns.network.com> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 32 In article <1317@ns.network.com> ddb@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@mimsy.umd.edu Path: uunet!mimsy!chris