473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

How can #define printf like function?

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.

Nov 14 '05 #1
5 27026
In article <11*********************@c13g2000cwb.googlegroups. com>,
Kevin <ke*****@126.com> wrote:
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.


The direct way requires a C99 compiler. Very brief description at
http://www.comeaucomputing.com/techt...variadicmacros
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 14 '05 #2
Kevin wrote:
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 )
Nov 14 '05 #3
Jens <je********@informatik.uni-oldenburg.de> wrote:
Kevin wrote:
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 )


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
Nov 14 '05 #4
In article <ct**********@newssrv2.hrz.uni-oldenburg.de>,
Jens <je********@informatik.uni-oldenburg.de> wrote:
Kevin wrote:
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 )


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.
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 14 '05 #5
Kevin wrote:
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

Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Bryan Parkoff | last post by:
"#define" can only be inside the global scope before main() function. "#if" can be tested after "#define" is executed. The problem is that "#define" can't be inside main() function. I do not wish...
3
by: Kelly | last post by:
Hi, Ashamed to ask basic question but I cannot get this code to run, #define Y one #include <stdio.h> #include <string.h> #include <stdlib.h>
15
by: Jorge Naxus | last post by:
Hello I would like to write a macro like that: #ifdef DEBUG #define printj(...) printf(...) #else #printj(...) #endif
9
by: pozz | last post by:
Hi all, I have the below #defines #define NUMBER1 30 #define NUMBER2 50 #define SUM (NUMBER1+NUMBER2) #define STRING1 "Byte: \x30" #define STRING2 "Byte: \x50"...
4
by: Allan Bruce | last post by:
Is there a way to make a pre-processor define with a variable number of arguements? I want to have something like this: #define DBG_WRITE(char *A, ...) { va_list vl; char dbgTmpStr;...
19
by: Sensei | last post by:
Hi! I'm concerned about the legality of such a definition: #define funcX funcY where funcX belongs to the *standard* C functions. Is it legal to do this? The standard says "any function...
4
by: Mohammad Omer Nasir | last post by:
I was read Linux kernel code in which I saw few define macro defines in functions. The snap of the function is following and file name "err_ev6.c". static int ev6_parse_cbox(u64 c_addr, u64...
28
by: ravi | last post by:
Hello everybody, I am writing a small application which does some work before the user main function starts execution. I am trying to #define the main function. But the problem is that,
2
by: Mant | last post by:
Hi, I have rewritten a printf function according to my usage and I want call my printf function instead the standard printf function during my project execution. Can anybody tell me what is the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.