具有可变长度参数的宏 [英] macro with variable length of argument

查看:85
本文介绍了具有可变长度参数的宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


如何编写一个可变长度的参数宏?

例如,如果我需要一个宏来检查返回值printf。


#define PRINTF_ESC(x,...)if(printf(x,...)== 10)goto end;


然而,它不起作用。 > _<


非常感谢。


Ethan

Hi All,

How to write a macro with variable length of argument?
For example, if i need a macro to check return value of printf.

#define PRINTF_ESC(x, ...) if(printf(x, ...) == 10) goto end;

However, it doesn''t work. >_<

Thank you very much.

Ethan

推荐答案

ethan在25/09/05写道:
ethan wrote on 25/09/05 :
如何编写一个可变长度的参数宏?


C90:


#define PRINTF(a)打印


用法:


PRINTF((我是%d \ n,49));

例如,如果我需要一个宏来检查printf的返回值。

#define PRINTF_ESC(x,...)if(printf(x,...)== 10)goto end;

但是,它不起作用。 > _<
How to write a macro with variable length of argument?
C90 :

#define PRINTF(a) print a

usage :

PRINTF (("Hi, I''m %d\n", 49));
For example, if i need a macro to check return value of printf.

#define PRINTF_ESC(x, ...) if(printf(x, ...) == 10) goto end;

However, it doesn''t work. >_<




它可能适用于C99。我不确定语法......


-

Emmanuel

C-FAQ: http://www.eskimo.com/~scs/C- faq / faq.html

C库: http://www.dinkumware.com/refxc.html


..sig正在修理



It may works with C99. I''m not sure of the syntax...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair


(取代< mn *********************** @ YOURBRAnoos.fr>)


ethan写道25/09/05:
(supersedes <mn***********************@YOURBRAnoos.fr>)

ethan wrote on 25/09/05 :
如何编写一个参数长度可变的宏?


C90:


#define PRINTF(a)printf a


用法:


PRINTF((我是%d \ n,49));

例如,如果我需要一个宏来检查printf的返回值。

#define PRINTF_ESC(x,...)if(printf(x,...)== 10)goto end;

但是,它不起作用。 > _<
How to write a macro with variable length of argument?
C90 :

#define PRINTF(a) printf a

usage :

PRINTF (("Hi, I''m %d\n", 49));
For example, if i need a macro to check return value of printf.

#define PRINTF_ESC(x, ...) if(printf(x, ...) == 10) goto end;

However, it doesn''t work. >_<




它可能适用于C99。我不确定语法......

-

Emmanuel

C-FAQ: http://www.eskimo.com/~scs/C-faq/faq。 html

C库: http: //www.dinkumware.com/refxc.html


今天世界上有10种类型的人;

那些理解二进制,以及那些不是。



It may works with C99. I''m not sure of the syntax...
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."


" ethan <运算***** @ gmail.com>写道:

#大家好,



#如何编写一个可变长度的参数宏?

#例如,如果我需要一个宏来检查printf的返回值。



#define PRINTF_ESC(x,...)if(printf(x) ,...)== 10)转到结尾;


你可以用双括号做到这一点。 (注意没有括号
printf后的
。)


@ more tc

#define PRINTF_ESC(x)if(printf x = = 10)转到结束;

PRINTF_ESC((a))

PRINTF_ESC((a,b,c,d))


@ cc -E tc

#1" t.c"

#1"< built-in>"

#1"<命令行>"

#1" t.c"


if(printf(a)== 10) goto end;

if(printf(a,b,c,d)== 10)goto end;

我认为gcc有一个扩展名,但它可能不是

可以在其他地方找到。


-

SM瑞恩 http://www.rawbw.com/~wyrmwif/

OOOOOOOOOO!海豹突击队!
"ethan" <op*****@gmail.com> wrote:
# Hi All,
#
# How to write a macro with variable length of argument?
# For example, if i need a macro to check return value of printf.
#
# #define PRINTF_ESC(x, ...) if(printf(x, ...) == 10) goto end;

You can do it with double parentheses. (Note no parentheses
after printf.)

@ more t.c
#define PRINTF_ESC(x) if(printf x == 10) goto end;
PRINTF_ESC((a))
PRINTF_ESC((a,b,c,d))

@ cc -E t.c
# 1 "t.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "t.c"

if(printf (a) == 10) goto end;
if(printf (a,b,c,d) == 10) goto end;
I think gcc has an extension for this, but it might not
be available elsewhere.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
OOOOOOOOOO! NAVY SEALS!


这篇关于具有可变长度参数的宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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