Boston Linux & Unix (BLU) Home | Calendar | Mail Lists | List Archives | Desktop SIG | Hardware Hacking SIG
Wiki | Flickr | PicasaWeb | Video | Maps & Directions | Installfests | Keysignings
Linux Cafe | Meeting Notes | Blog | Linux Links | Bling | About BLU

BLU Discuss list archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

C Macro question

I write C code according to the C standard. vfprintf(3) is a standard 
function available on all standard C implementations. C preprocessor macros 
according to the standard do not support variable arguments. There are ways 
to get around this, but I dod not want to be overly complicated.
"Derek D. Martin" wrote:
> Hash: SHA1
> At some point hitherto, Jerry Feldman hath spake thusly:
> > In short, macros do not support variable arguments, but....
> Well, at least with Gnu tools, yes they do.  See my previous post.  I
> haven't tried this with any proprietary compiler, as I currently don't
> have access to any.
> > You are better off using a C function for that (assume you include 
> > stdarg.h).
> > int printerr(const star *fmt, ...)
> > {
> > 	va_list ap;
> > 	int rv;
> > 	time_t now = time(NULL);
> > 	char msg[SOMESIZE];
> > 	va_start(ap, fmt);
> > 	sprintf(msg, "%s - ERROR: %s", ctime(&now), fmt);
> > 	rv = vfprintf(logfile, msg, ap);
> > 	return rv;
> > }
> This reminds me; your implementation (if you're not using glibc) may
> not have vsnprintf().  If you're writing to a file, you can use a
> method something like gaf's above.  Most implementations have
> vfprintf() which make that possible.
> If you're using some other API, such as to log a message to syslog,
> that won't work (because you need to have the message you're logging
> in a buffer to pass to syslog()).  In such a case, you can borrow the
> source for vsnprintf() from the glibc source.  Many Gnu programs
> explicitly include the source for functions added by the most recent C
> standards (such as vsnprintf()), as many older implementations don't
> have them.  Chances are fair that if you've got the source for any Gnu
> program laying around, the code for vsnprintf() might be with it.
> - -- 
> Derek Martin               ddm at    
> - ---------------------------------------------
> I prefer mail encrypted with PGP/GPG!
> GnuPG Key ID: 0x81CFE75D
> Retrieve my public key at
> Learn more about it at
> Version: GnuPG v1.0.6 (GNU/Linux)
> Comment: For info see
> iD8DBQE9DPtedjdlQoHP510RAoCDAJ9229nJvYv+0++OEzk9iYcuuT9AiwCgj29r
> BOyJKvoFQHg74CzRL9giFKQ=
> =02N5
> _______________________________________________
> Discuss mailing list
> Discuss at

Jerry Feldman <gaf at>
Boston Linux and Unix user group PGP key id:C5061EA9
PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9

BLU is a member of BostonUserGroups
BLU is a member of BostonUserGroups
We also thank MIT for the use of their facilities.

Valid HTML 4.01! Valid CSS!

Boston Linux & Unix /