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

Re: c macro





Evgeny Stambulchik wrote:

> Erez Doron <erez@savan.com> wrote:
>
> >  I'm tring to make any  on that uses printf to
> >  use sprintf instead
> >
> >  somthing like:
> >
> >  #ifdef LINUX
> >  #define printf(...) {sprintf(s,...);do_print(s);}
> >  #endif
>
> static char s[256];
> #define printf(args... ) sprintf(s, ## args); do_print(s)
>
> Notice that it's a GCC extension; in the ANSI C there is no way of doing such a
> thing.
>

There is a way to do this with the ANSI C preprocessor assuming that you are
willing to live with
a small restriction, namely - putting the list of varargs in parens. The
pre-processor will chew that
easily. For example:

Try:

#define printf(s, fmt, args)   \
{sprintf(s, fmt, args); do_print(s);}

When you call this macro, you MUST put parens around your argument list in
order to pass the pre-processor. For example:

const char* aptr = "Hello";
const char* bptr = "World";
printf(buf, "%s %s", (aptr, bptr));

This should do it (you can even embed __LINE__, and __FILE__ in your macro
and have them expand automatically to the current line in the current file).


Regards



begin:          vcard
fn:             Liron Levy
n:              Levy;Liron
org:            Pointer Software Systems Ltd.
adr;dom:        Alexander Zaid 14 st.;;;Kiryat Tivon;ISRAEL;36021;
email;internet: liron@pointer.co.il
title:          Software Developer
tel;work:        972-4-9833736
tel;fax:        972-4-9836547
x-mozilla-cpt:  ;0
x-mozilla-html: TRUE
version:        2.1
end:            vcard