关于#define的问题 [英] Question about #define

查看:95
本文介绍了关于#define的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


我想写一个像这样的宏:


#ifdef DEBUG

# define printj(...)printf(...)

#else

#printj(...)

#endif


因此当DEBUG为真时,printj的行为与printf完全相同,并且当DEBUG为假时,
不会做任何事情。

但我不知道如何处理

printf(...)的可变数量的参数。


任何有关如何成为我的定义的帮助?


谢谢,


Jorge

Hello

I would like to write a macro like that:

#ifdef DEBUG
#define printj(...) printf(...)
#else
#printj(...)
#endif

So that printj behaves exactly like printf when DEBUG is true and
doesnt do anything when DEBUG is false.
But I dont know how to deal with the variable number of arguments of
printf(...).

Any help on how should be my "define"??

Thanks,

Jorge

推荐答案

你可以使用:


#ifdef DEBUG

#define printj(x)printf((x))

#else

#define printj(x)

#endif


但现在你应该使用printj as:printj((只是一个测试\\ n));


AFAIK宏不支持可变数量的参数。


Frank


Jorge Nax美国" <例如******* @ terra.es>在消息中写道

news:dc ************************** @ posting.google.c om ...
You can use:

#ifdef DEBUG
#define printj(x) printf((x))
#else
#define printj(x)
#endif

but now you should use printj as: printj(("just a test\n"));

AFAIK macros don''t support a variable number of arguments.

Frank

"Jorge Naxus" <eg*******@terra.es> wrote in message
news:dc**************************@posting.google.c om...
你好

我想写一个像这样的宏:

#ifdef DEBUG
#define printj(...) printf(...)
#else
#printj(...)
#endif

当DEBUG为真时,printj的行为与printf完全相同当DEBUG错误时,它不会做任何事情。
但我不知道如何处理
printf(...)的可变数量的参数。

任何帮助如何应该是我的定义?

谢谢,

Jorge
Hello

I would like to write a macro like that:

#ifdef DEBUG
#define printj(...) printf(...)
#else
#printj(...)
#endif

So that printj behaves exactly like printf when DEBUG is true and
doesnt do anything when DEBUG is false.
But I dont know how to deal with the variable number of arguments of
printf(...).

Any help on how should be my "define"??

Thanks,

Jorge



On 2004年3月1日07:10:01 -0800, eg*******@terra.es (豪尔赫Naxus)写道:
On 1 Mar 2004 07:10:01 -0800, eg*******@terra.es (Jorge Naxus) wrote:
你好

我想写一个这样的宏:

#ifdef DEBUG
#define printj(...)printf(...)
#else
#printj(...)
#endif

这样printj表现得准确当DEBUG为真时就像printf一样,当DEBUG为假时没有做任何事情。
但是我不知道如何处理
printf(...)的可变数量的参数。 />
关于应该如何成为我的定义的任何帮助?


C99确实如此(正如我刚刚通过Google搜索完全惊讶地发现的那样)

支持可变参数宏!所以,如果你在C99中编码,你就完全没有了。


否则,我可能会通过创建普通(非宏)来实现这个目标。

variadic printj函数,其中,通过条件编译

(可能基于你的DEBUG符号),或者转到

printf-family函数(例如vprintf)或者不是。

-leor

谢谢,

Jorge
Hello

I would like to write a macro like that:

#ifdef DEBUG
#define printj(...) printf(...)
#else
#printj(...)
#endif

So that printj behaves exactly like printf when DEBUG is true and
doesnt do anything when DEBUG is false.
But I dont know how to deal with the variable number of arguments of
printf(...).

Any help on how should be my "define"??
C99 does (as I just discovered to my complete surprise by Googling)
support variadic macros! So if you''re coding in C99 you''re all set.

Otherwise, I''d probably approach this by creating an ordinary (non-macro)
variadic printj function inside of which, via conditional compilation
(based presumably on your DEBUG symbol), either hands off to a
printf-family function (e.g. vprintf) or doesn''t.
-leor

Thanks,

Jorge




Leor Zolman

BD软件
le**@bdsoft.com
www.bdsoft.com - C / C ++,Java,Perl& amp; ; Unix

C ++用户:下载BD Software的免费STL错误消息

Decryptor at www.bdsoft.com/tools/stlfilt.html



Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software''s free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html


Jorge Naxus写道:
Jorge Naxus wrote:

我想写一个像这样的宏:

#ifdef DEBUG
#define printj(...)printf(...)
#else
#printj(...)
#endif

因此,当DEBUG为真时,printj的行为与printf完全相同,并且当没有做任何事情时DEBUG是错误的。但我不知道如何处理printf(...)的可变数量的参数。

任何关于应该如何成为我的define的帮助??

I would like to write a macro like that:

#ifdef DEBUG
#define printj(...) printf(...)
#else
#printj(...)
#endif

So that printj behaves exactly like printf when DEBUG is true and
doesnt do anything when DEBUG is false. But I dont know how to
deal with the variable number of arguments of printf(...).

Any help on how should be my "define"??




您有两种选择。


1.获取并使用C99编译器。然后你的代码将不再将
移植到C90编译器。


2.获取并使用gcc和gnu可变参数宏扩展。然后

您的代码将不再移植到非gnu编译器。如果你得到一个

足够晚的gcc你可以结合选项1,因为最新的

gccs除了gnu风味之外还处理C99可变参数宏。


我能告诉你的唯一例子是nmalloc.zip,可以在我的

页面下载部分找到。它正在使用那些设施,因此
仅限于gcc编译。


-

Chuck F(cb * *******@yahoo.com)(cb********@worldnet.att.net)

可用于咨询/临时嵌入式和系统。

< http://cbfalconer.home.att.net>使用worldnet地址!



You have two choices.

1. Get and use a C99 compiler. Then your code will no longer port
to a C90 compiler.

2. Get and use gcc and the gnu variadic macro extensions. Then
your code will no longer port to a non-gnu compiler. If you get a
late enough gcc you can combine with option 1, because the latest
gccs handle C99 variadic macros in addition to the gnu flavor.

The only example I can show you is in nmalloc.zip, available on my
page, download section. It is using exactly those facilities, and
is thus limited to gcc compilation.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


这篇关于关于#define的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆