Path: utzoo!utgpu!water!watmath!clyde!rutgers!cmcl2!brl-adm!adm!Alan_T._Cote.OsbuSouth@Xerox.COM From: Alan_T._Cote.OsbuSouth@Xerox.COM Newsgroups: comp.lang.c Subject: Re: Varargs for Macros? Message-ID: <11925@brl-adm.ARPA> Date: 21 Feb 88 01:03:07 GMT Sender: news@brl-adm.ARPA Lines: 33 Dan Kegel writes, >For example, I'd like to have a macro that calls write() and prints a fancy >error message upon failure. It would expand this > WRITE(fd, buf, bytes, "Error at record %d of %s", recNum, fileName); >to this: > if (write(fd, buf, bytes) != bytes) > fprintf(stderr, "Error at record %d of %s", recNum, fileName), exit(1); >regardless of the number of arguments to fprintf. If you're willing to modify your example just a little, you can get what you want. Try this macro: #define WRITE(fd,buf,bytes,errparm) \ if(write(fd,buf,bytes)!=bytes) \ fprintf errparm, exit(1) Here's a sample call: WRITE(fd, buf, bytes, (stderr,"Error at record %d of %s", recNum, fileName)); Which should generate: if(write(fd,buf,bytes)!=bytes) fprintf (stderr,"Error at record %d of %s", recNum, fileName), exit(1); The trick is in the use of parentheses. Neat, huh?!? - Al Cote' PS: No flames for tasteless style -- this is only a quickie!!