Varargs for Macros?

Dan Kegel dan at srs.UUCP
Sat Feb 20 06:37:46 AEST 1988


We now have a nice standard way of writing functions that take variable
numbers of arguments.  Is there any corresponding capability for macros?

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.

This example is a little poor, I admit, but it demonstrates passing a
variable number of parameters to a macro.

I suppose the macro definition might use elipses:
#define WRITE(i, p, n, s, ...)     \
    if (write(i, p, n) != n)       \
	fprintf(stderr, s, ...),   \
	exit(1)

Anybody else ever want to do this sort of thing?
-- 
   Dan Kegel
   srs!dan at cs.rochester.edu  dan%srs.uucp at cs.rochester.edu  rochester!srs!dan



More information about the Comp.lang.c mailing list