How can #define printf like function?

K

Kevin

Hi every one.
I want to use marco '#define' define a function like printf that have
variable arguments, do it possible?
it may be like that, I write a function
foo(int LineNo, char * fmt, ...)
I want define
#define MyFoo(fmt, ...) foo(__LINE__, fmt, ...)
but it can't works.

Any help will be appreciated.
 
J

Jens

Kevin said:
Hi every one.
I want to use marco '#define' define a function like printf that have
variable arguments, do it possible?

I'm using gcc 2.95. and this is my debug function:


#define ERR(n, a...) deb_err( __LINE__, __FILE__, __FUNCTION__,n , ## a )
void deb_err( int line, const char* file, const char *function, const
char *format, ... );

note the ", ## a" the macro even works if no variable argument list is
given.the precompile will remove the comma ",".

btw. you could even convert arguments to strings and print them.
#define ASERR(n,f,a...) do { if(!(n)) ERR( "ASSERT:"#n"\n"#f, ##a ); }
while(0)

the conversion is done with "#n"
e.g.: ASERR( x==0, "x should be zero but it is x=%d", x )
 
R

Richard Bos

Jens said:
I'm using gcc 2.95. and this is my debug function:

#define ERR(n, a...) deb_err( __LINE__, __FILE__, __FUNCTION__,n , ## a )

That's very pretty, but it isn't C. IIRC it's correct Ganuck, but ISO C
does not provide it. C99 _does_ provide a different syntax for what the
OP wants (see Greg's post), but C89 doesn't provide any solution. Yours
is a syntax error in any standard version of C.

Richard
 
G

Greg Comeau

I'm using gcc 2.95. and this is my debug function:

#define ERR(n, a...) deb_err( __LINE__, __FILE__, __FUNCTION__,n , ## a )
void deb_err( int line, const char* file, const char *function, const
char *format, ... );

note the ", ## a" the macro even works if no variable argument list is
given.the precompile will remove the comma ",".

btw. you could even convert arguments to strings and print them.
#define ASERR(n,f,a...) do { if(!(n)) ERR( "ASSERT:"#n"\n"#f, ##a ); }
while(0)

the conversion is done with "#n"
e.g.: ASERR( x==0, "x should be zero but it is x=%d", x )

That will work, but requires gcc, or rather a compiler
which supports gcc's extended variadic macros
(which obviously would be gcc/g++, and also Comeau C/Comeau C++ in
GNU C/GNU C++ mode respectively).

As in my other post, there is a C99 way, which also obviously
requires a C99 compiler, or at least a compiler supporting
C99 variadic macros (which at least gcc and Comeau also support).

In C89, there is no direct way to accomplish this, and requires
some inventiveness, if even in some cases.
 
S

Shawn

Kevin said:
Hi every one.
I want to use marco '#define' define a function like printf that have
variable arguments, do it possible?
it may be like that, I write a function
foo(int LineNo, char * fmt, ...)
I want define
#define MyFoo(fmt, ...) foo(__LINE__, fmt, ...)
but it can't works.

Any help will be appreciated.

You can use this technique to write macros that take variable number of
arguments.

#define MyFoo(a) foo a
#define MyPrintf(a) printf a

To use these macros, use two parenthesis instead of just one.

MyFoo((__LINE__, "blah %d", 25));
MyPrintf(("This is a test: %d", 25));


- Shawn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top