#define宏用于包含带有字符串的旧宏 [英] #define macro to enclose an older macro with strings

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

问题描述

嘿嘿,我会尽量保持这个简短;)


这是我想要输入的内容(或者至少也是关闭的)......


#define VER_BUILD 1

#define STR_VER_BUILD" VER_BUILD"

但是预处理器会看到STR_VER_BUILD中的小报并且

用VER_BUILD替换该文本...

我需要它来查看VER_BUILD并将其替换为1,并且仅在执行
$之后b $ b替换将1包括在quots中...

我尝试使用数字符号而没有任何运气(编号在宏中签名

需要params包围下一个quots in Quots)。


#define VER_BUILD 1

#define THING_TO_STR(thing)#thing

#define STR_VER_BUILD THING_TO_STR( VER_BUILD)


这两次尝试都给了我STR_VER_BUILD替换为

" VER_BUILD" ...而不是我想要的... VER_用1

代替BUILD,STR_VER_BUILD替换为1


就这样没有任何混淆......任何其他地方都是我单独使用VER_BUILD

,预处理器用1代替。

Hey people, i''ll try to keep this short ;)

Here is what I want to type (or at least close too)...

#define VER_BUILD 1
#define STR_VER_BUILD "VER_BUILD"

But what happends is the preprocessor see the quots in STR_VER_BUILD and
replaces that text with "VER_BUILD"...
I need it to see the VER_BUILD and replace it with 1, and only after doing
that replacement enclose the 1 in quots...
I tried using a number sign without any luck (number sign in a macro that
takes params encloses the next param in quots).

#define VER_BUILD 1
#define THING_TO_STR(thing) # thing
#define STR_VER_BUILD THING_TO_STR(VER_BUILD)

Both these tries gave me the result of STR_VER_BUILD being replaced with
"VER_BUILD"... Instead of what I wanted... VER_BUILD being replaced with 1
and STR_VER_BUILD being replaced with "1"

Just so there isn''t any confusion... any other place were i used VER_BUILD
alone, the preprocessor replaced that with 1.

推荐答案



" ;死RAM <德***** @ hotmail.com>在消息中写道

新闻:1o ******************** @ twister01.bloor.is.net .cable.rogers.com .. 。

"Dead RAM" <de*****@hotmail.com> wrote in message
news:1o********************@twister01.bloor.is.net .cable.rogers.com...
嘿人们,我会尽量保持这个简短;)

这是我想要输入的内容(或者至少也是关闭的)...

#define VER_BUILD 1
#define STR_VER_BUILD" VER_BUILD"

但是发生的事情是预处理器看到STR_VER_BUILD中的小数并用
替换该文本; VER_BUILD" ...
我需要它来查看VER_BUILD并将其替换为1,并且只有在做了替换后才能将1替换为quots ...
我尝试使用一个数字没有任何运气的标志(在一个宏中的数字标志,
需要参数括起下一个参数)。

#define VER_BUILD 1
#define THING_TO_STR(thing)#thing
#define STR_VER_BUILD THING_TO_STR(VER_BUILD)

这两次尝试都给了我STR_VER_BUILD替换为
&q的结果uot; VER_BUILD" ...而不是我想要的......用1
替换VER_BUILD,用1替换STR_VER_BUILD
Hey people, i''ll try to keep this short ;)

Here is what I want to type (or at least close too)...

#define VER_BUILD 1
#define STR_VER_BUILD "VER_BUILD"

But what happends is the preprocessor see the quots in STR_VER_BUILD and
replaces that text with "VER_BUILD"...
I need it to see the VER_BUILD and replace it with 1, and only after doing
that replacement enclose the 1 in quots...
I tried using a number sign without any luck (number sign in a macro that
takes params encloses the next param in quots).

#define VER_BUILD 1
#define THING_TO_STR(thing) # thing
#define STR_VER_BUILD THING_TO_STR(VER_BUILD)

Both these tries gave me the result of STR_VER_BUILD being replaced with
"VER_BUILD"... Instead of what I wanted... VER_BUILD being replaced with 1
and STR_VER_BUILD being replaced with "1"




是C预处理器的一个很好的例子。这个

小变种有效


#define VER_BUILD 1

#define _THING_TO_STR(thing)#thing

#define THING_TO_STR(东西)_THING_TO_STR(东西)

#define STR_VER_BUILD THING_TO_STR(VER_BUILD)


别问我解释原因,因为我不知道。这只是一个值得知道的技巧。


john



This is a good example of the weirdness that is the C pre-processor. This
minor variation works

#define VER_BUILD 1
#define _THING_TO_STR(thing) # thing
#define THING_TO_STR(thing) _THING_TO_STR(thing)
#define STR_VER_BUILD THING_TO_STR(VER_BUILD)

Don''t ask me to explain why because I don''t know. It just a trick worth
knowing.

john


>
#define VER_BUILD 1
#define _THING_TO_STR(thing)#thing
#define THING_TO_STR(thing)_THING_TO_STR(thing)
#define STR_VER_BUILD THING_TO_STR(VER_BUILD)
#define VER_BUILD 1
#define _THING_TO_STR(thing) # thing
#define THING_TO_STR(thing) _THING_TO_STR(thing)
#define STR_VER_BUILD THING_TO_STR(VER_BUILD)




在其他人指出之前,带有前导下划线的标识符

后跟大写字母是_NOT_ALLOWED_。将_THING_TO_STR替换为

更合适的东西。


john



Before some else points it out, identifiers with a leading underscore
followed by uppercase letter are _NOT_ALLOWED_. Replace _THING_TO_STR with
something more suitable.

john


Dead RAM写道:
Dead RAM wrote:
嘿人们,我会尽量保持这个简短;)

这是我想要输入的内容(或者至少也是关闭的)...

#define VER_BUILD 1
#define STR_VER_BUILD" VER_BUILD"

但是发生的事情是预处理器看到STR_VER_BUILD中的小数并用
替换该文本; VER_BUILD" ...
我需要它来查看VER_BUILD并将其替换为1,并且只有在做了替换后才能将1替换为quots ...
我尝试使用一个数字没有任何运气的标志(在一个宏中的数字标志,
需要参数括起下一个参数)。

#define VER_BUILD 1
#define THING_TO_STR(thing)#thing
#define STR_VER_BUILD THING_TO_STR(VER_BUILD)

这两次尝试都给了我结果STR_VER_BUILD被替换为
VER_BUILD...而不是我想要的... VER_BUILD被替换为1
而STR_VER_BUILD被替换为1

所以没有任何混淆......任何其他地方我单独使用VER_BUILD
,预处理器用1代替。
Hey people, i''ll try to keep this short ;)

Here is what I want to type (or at least close too)...

#define VER_BUILD 1
#define STR_VER_BUILD "VER_BUILD"

But what happends is the preprocessor see the quots in STR_VER_BUILD and
replaces that text with "VER_BUILD"...
I need it to see the VER_BUILD and replace it with 1, and only after doing
that replacement enclose the 1 in quots...
I tried using a number sign without any luck (number sign in a macro that
takes params encloses the next param in quots).

#define VER_BUILD 1
#define THING_TO_STR(thing) # thing
#define STR_VER_BUILD THING_TO_STR(VER_BUILD)

Both these tries gave me the result of STR_VER_BUILD being replaced with
"VER_BUILD"... Instead of what I wanted... VER_BUILD being replaced with 1
and STR_VER_BUILD being replaced with "1"

Just so there isn''t any confusion... any other place were i used VER_BUILD
alone, the preprocessor replaced that with 1.




#include< ; stdio.h>

#include< stdlib.h>

#define STR_VER_BUILD" 1"

#define VER_BUILD atoi(STR_VER_BUILD )

int main(无效){

printf("%d = \"%s \" \ n",VER_BUILD,STR_VER_BUILD);

返回0;

}



#include <stdio.h>
#include <stdlib.h>
#define STR_VER_BUILD "1"
#define VER_BUILD atoi(STR_VER_BUILD)
int main(void) {
printf("%d = \"%s\"\n", VER_BUILD, STR_VER_BUILD);
return 0;
}


这篇关于#define宏用于包含带有字符串的旧宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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