预处理器q:不可能的宏? [英] preprocessor q: impossible macro?

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

问题描述

大家好,

我已经得到了这行代码(不能改变这个;向导生成,但

值有朝一日可能会改变):

#define IDB_BUTTON 225


代码中的某个地方我发现/需要这个:

..... somefunction(&# 225)....


函数调用中的这个字符串与define中的字符串相同,但

常量未被使用。因为为了将来的更改和可读性,我想要用这样的预处理器宏来创建这个字符串:

..... somefunction(MYMACRO(IDB_BUTTON)).. ..


我没有得到这样的宏工作。我试过了:

#define MYMACRO(num)"#" #num

但这只会产生

..... somefunction(#,IDB_BUTTON)....

我也尝试过使用令牌粘贴操作员(##)等。


这可能吗?

Eric

Hello all,
I''ve got this line of given code (cannot change this; wizard-generated, but
value may change someday):
#define IDB_BUTTON 225

Somewhere in the code I found / need this:
.....somefunction("#225")....

This string in the function call is the same number like in the define, but
the constant wasn''t used. Because for future changes and for readability I
wanted to create this string with a preprocessor macro like this:
.....somefunction(MYMACRO(IDB_BUTTON))....

I didn''t get such a macro to work. I tried:
#define MYMACRO(num) "#" #num
but this only yields to
.....somefunction("#" "IDB_BUTTON")....
I also tried with the token-pasting operator (##) etc.

Is this possible at all?
Eric

推荐答案

Eric写道:
我有这行代码(不能改变这个;向导生成,但是
价值可能会在某一天发生变化):
#define IDB_BUTTON 225

代码中的某处我发现/需要这个:
.... somefunction(#225).. ..

函数调用中的这个字符串与define中的字符串相同,但
该常量未被使用。因为为了将来的更改和可读性,我想用这样的预处理器宏创建这个字符串:
.... somefunction(MYMACRO(IDB_BUTTON))....
我没有得到这样的宏观工作。我试过了:
#define MYMACRO(num)"#" #num
但这只会产生
.... somefunction(#" IDB_BUTTON&qu​​ot;)....
我也尝试过使用令牌粘贴操作符(# #)等

这有可能吗?
I''ve got this line of given code (cannot change this; wizard-generated, but
value may change someday):
#define IDB_BUTTON 225

Somewhere in the code I found / need this:
....somefunction("#225")....

This string in the function call is the same number like in the define, but
the constant wasn''t used. Because for future changes and for readability I
wanted to create this string with a preprocessor macro like this:
....somefunction(MYMACRO(IDB_BUTTON))....

I didn''t get such a macro to work. I tried:
#define MYMACRO(num) "#" #num
but this only yields to
....somefunction("#" "IDB_BUTTON")....
I also tried with the token-pasting operator (##) etc.

Is this possible at all?




是的,你需要一个间接的字符串化宏观。


#define STR(x)#x

#define MYMACRO(num)"#" ## STR(num)


#define IDB_BUTTON 225

#include< stdio.h>


int main(无效){

printf(" Got%s \ nn",MYMACRO(IDB_BUTTON));

}


V



Yes, you need an indirect "stringizing" macro for that.

#define STR(x) #x
#define MYMACRO(num) "#" ## STR(num)

#define IDB_BUTTON 225

#include <stdio.h>

int main(void) {
printf("Got %s\n", MYMACRO(IDB_BUTTON));
}

V


Victor Bazarov写道:
Victor Bazarov wrote:
Eric写道:
我已经得到这行给定的代码(不能改变这个;向导生成,但
值可能有一天会改变):
#define IDB_BUTTON 225

代码中的某处我发现/需要这个:
.... somefunction(#225)....

函数调用中的这个字符串与定义中的数字相同,但是
常数没用过。因为为了将来的更改和可读性,我想用这样的预处理器宏创建这个字符串:
.... somefunction(MYMACRO(IDB_BUTTON))....
我没有得到这样的宏观工作。我试过了:
#define MYMACRO(num)"#" #num
但这只会产生
.... somefunction(#" IDB_BUTTON&qu​​ot;)....
我也尝试过使用令牌粘贴操作符(# #)等

这有可能吗?
I''ve got this line of given code (cannot change this; wizard-generated, but
value may change someday):
#define IDB_BUTTON 225

Somewhere in the code I found / need this:
....somefunction("#225")....

This string in the function call is the same number like in the define, but
the constant wasn''t used. Because for future changes and for readability I
wanted to create this string with a preprocessor macro like this:
....somefunction(MYMACRO(IDB_BUTTON))....

I didn''t get such a macro to work. I tried:
#define MYMACRO(num) "#" #num
but this only yields to
....somefunction("#" "IDB_BUTTON")....
I also tried with the token-pasting operator (##) etc.

Is this possible at all?



是的,你需要一个间接的字符串化宏观。

#define STR(x)#x
#define MYMACRO(num)"#" ## STR(num)

#define IDB_BUTTON 225
#include< stdio.h>

int main(void){
printf(" Got%s \ n",MYMACRO(IDB_BUTTON));
}

V



Yes, you need an indirect "stringizing" macro for that.

#define STR(x) #x
#define MYMACRO(num) "#" ## STR(num)

#define IDB_BUTTON 225

#include <stdio.h>

int main(void) {
printf("Got %s\n", MYMACRO(IDB_BUTTON));
}

V




以上解决方案无法编译。由于粘贴了相邻的字符串文字,因此不需要粘贴代码




#define STR(x)#x

#define MYMACRO(num)# STR(num)


#define IDB_BUTTON 225

#include< stdio.h>


int main(void){

printf(" Got%s \ nn",MYMACRO(IDB_BUTTON));

}

-Charlie



The above solution does not compile. Token pasting is not necessary
since adjacent string literals are pasted.

#define STR(x) #x
#define MYMACRO(num) "#" STR(num)

#define IDB_BUTTON 225

#include <stdio.h>

int main(void) {
printf("Got %s\n", MYMACRO(IDB_BUTTON));
}

-Charlie


Charles Mills写道:
Charles Mills wrote:
Victor Bazarov写道:
[...]
Victor Bazarov wrote:
[..]

#define STR(x)#x
#define MYMACRO(num)"#" ## STR(num)

#define IDB_BUTTON 225
#include< stdio.h>

int main(void){
printf(" Got%s \ n",MYMACRO(IDB_BUTTON));
}

V

#define STR(x) #x
#define MYMACRO(num) "#" ## STR(num)

#define IDB_BUTTON 225

#include <stdio.h>

int main(void) {
printf("Got %s\n", MYMACRO(IDB_BUTTON));
}

V



以上解决方案不编译。 [..]


The above solution does not compile. [..]




关于什么编译器?什么是错误信息?


V



On what compiler? What is the error message?

V


这篇关于预处理器q:不可能的宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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