宏疑问 [英] macro doubt

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

问题描述

是否可以将变量no.of参数传递给宏...


#define print(a,b)printf(a,b)


void main()

{

int a = 1,b = 2,c = 3;

打印(%d%d%d,(a,b,c));

}


上面代码中的问题是表达式(a,b,c)评估为


并且只打印c的值。

是否可以将变量no.of参数传递给宏...


不在目前实施的标准C中。如果你是幸运和独特的足够的b
,请在宏文本中使用...并在
中使用__VA_ARGS__替换。

#define print(a,b)printf(a,b)

void main()




main返回int。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


sinbad写道:

是否可以将变量no.of参数传递给宏...


1999年采用的标准规定了这一点;来自

1989(1990)的旧标准没有。一些符合1989年(1990年)标准的编制者有这样的机制;有些看起来像1999

标准机制,可能可以移植到C99实现,但

其他人使用的语法或语义不同于199

标准。所以要小心使用它们,仔细阅读文档,

如果你希望在你的代码中使用1989(1990)编译器,那么<​​br />
根本不使用它们。


但是...

void main()



^^^^

*否* C的标准版本(或标准前的K& RC)允许使用非特定托管的

程序

实现。如果您的实现静默接受,请记住

您的下一个编译器(或者甚至是当前版本的下一个版本)

可以免费呕吐此非标准返回类型。

main在托管实现中可移植的任何C程序中返回一个int。


sinbad< si ********* **@gmail.com>写道:

是否可以将变量no.of参数传递给宏...




如上所述,不是在C89。但是,在某些情况下,可能会以这种限制来克服你的方式

。例如,


#define MY_ASSERT(cond,msg)if(cond){printf msg; abort();}


可以像


MY_ASSERT一样使用(4 == 4,(这将永远不会被打印。 ;));





MY_ASSERT(some_var == 4,(错误,some_var不是4(%u) ;,some_var));


此类解决方案可能很有用但风格并不总是可接受的。 YMMV。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。


is it possible to pass variable no.of arguments to a macro ...

#define print(a,b) printf(a,b)

void main()
{
int a=1,b=2,c=3;
print("%d %d %d",(a,b,c));
}

the problem in the above code is the expression (a,b,c) evaluates to c
,
and only the value of c will get printed.

解决方案

sinbad said:

is it possible to pass variable no.of arguments to a macro ...
Not in standard C as currently implemented. If you are fortunate and unique
enough to have a C99 compiler, use ... in the macro text and __VA_ARGS__ in
the replacement.
#define print(a,b) printf(a,b)

void main()



main returns int.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


sinbad wrote:

is it possible to pass variable no.of arguments to a macro ...
The standard adopted in 1999 provides for this; the old standard from
1989(1990) does not. Some compilers which otherwise conform to the
1989(1990) standard have mechanisms for this; some look like the 1999
standard mechanism and might be portable to a C99 implementation, but
others use syntax or semantics differing from those adopted in the 199
standard. So use these with care, reading the documentation carefully,
and if you expect ever to use a 1989(1990) compiler with your code, then
use them not at all.

But ...
void main()


^^^^
*NO* standard version of C (or the pre-standard K&R C) allows this for
programs which will be compiled with an non-specific hosted
implementation. If your implementation accepts this silently, remember
that your next compiler (or even the next revision of your current one)
is free to vomit at this non-standard return type.
main returns an int in any C program portable across hosted implementations.


sinbad <si***********@gmail.com> wrote:

is it possible to pass variable no.of arguments to a macro ...



As stated, not in C89. It''s possible in some cases to kludge your way
around this limitation, however. For example,

#define MY_ASSERT( cond, msg ) if( cond ) {printf msg; abort();}

can be used like

MY_ASSERT( 4 == 4, ("This will never be printed.") );

and

MY_ASSERT( some_var == 4, ("Error, some_var was not 4 (%u)",some_var) );

Solutions of this sort may be useful but are not always stylistically
acceptable. YMMV.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


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

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