使用宏将常量连接到常量字符串 [英] concatenate a constant to constant string using macros
问题描述
如何连接哈希定义的恒定值到另一个哈希
定义常量字符串。例如
#define ABC 100
#define MYSTR" ABC的价值是
现在我需要一个将ABC的值连接到MYSTR的字符串。
我在编译时需要这个。
结果常量字符串应该是ABC的价值是100
我怎么能这样做。
谢谢
sinbad
how to concatenate a "hash defined" constant value to another "hash
defined" constant string. For example
#define ABC 100
#define MYSTR "The value of ABC is"
Now i need a string that will concatenate the value of ABC to MYSTR .
I need this at compile time.
The resultant constant string should be "The value of ABC is 100"
How can i do this.
thanks
sinbad
推荐答案
" sinbad" < si *********** @ gmail.com写信息
新闻:76 ******************* *************** @ x19g2000 prg.googlegroups.com ...
"sinbad" <si***********@gmail.comwrote in message
news:76**********************************@x19g2000 prg.googlegroups.com...
hi,
如何连接哈希定义的恒定值到另一个哈希
定义常量字符串。例如
#define ABC 100
#define MYSTR" ABC的价值是
现在我需要一个将ABC的值连接到MYSTR的字符串。
我在编译时需要这个。
结果常量字符串应该是ABC的价值是100
我怎么能这样做。
how to concatenate a "hash defined" constant value to another "hash
defined" constant string. For example
#define ABC 100
#define MYSTR "The value of ABC is"
Now i need a string that will concatenate the value of ABC to MYSTR .
I need this at compile time.
The resultant constant string should be "The value of ABC is 100"
How can i do this.
你可以这样做:
__________________________________________________ ___________________
#include< stdio.h>
#define QUOTE_X(t)#t
#define QUOTE(t)QUOTE_X(t)
#define ABC 100
#define MYSTR" ABC的价值是
int main(){
char const concat [ ] = MYSTR " QUOTE(ABC);
printf("%s \ n",concat);
/ * --------- -------------------------------------------------- - * /
put(" \\\\ _______________________ \
___________________________________ \ npress< ENTER to exit ...");
getchar();
返回0;
}
__________________________________________________ ___________________
You can do something like:
__________________________________________________ ___________________
#include <stdio.h>
#define QUOTE_X(t)#t
#define QUOTE(t)QUOTE_X(t)
#define ABC 100
#define MYSTR "The value of ABC is"
int main() {
char const concat[] = MYSTR " " QUOTE(ABC);
printf("%s\n", concat);
/*------------------------------------------------------------*/
puts("\n\n_______________________\
___________________________________\npress <ENTERto exit...");
getchar();
return 0;
}
__________________________________________________ ___________________
sinbad写道:
sinbad wrote:
hi,
如何连接hash defined"恒定值到另一个哈希
定义常量字符串。例如
#define ABC 100
#define MYSTR" ABC的价值是
现在我需要一个将ABC的值连接到MYSTR的字符串。
我在编译时需要这个。
结果常量字符串应该是ABC的价值是100
我怎么能这样做。
how to concatenate a "hash defined" constant value to another "hash
defined" constant string. For example
#define ABC 100
#define MYSTR "The value of ABC is"
Now i need a string that will concatenate the value of ABC to MYSTR .
I need this at compile time.
The resultant constant string should be "The value of ABC is 100"
How can i do this.
这是comp.lang.c中的问题11.17。经常
问题(FAQ)列表在< http:// www .c-faq.com /> ;.
那里给出的解决方案适用于你所展示的ABC,
但是如果你有像这样的东西>
#define ABC(24 + 18)
你会得到ABC的价值是(24 + 18),而不是... 42。
-
Eric Sosman
es ***** @ ieee-dot-org.inva 盖子
6月18日,8:01 * am,Chris Thomasson < cris ... @ comcast.netwrote:
On Jun 18, 8:01*am, "Chris Thomasson" <cris...@comcast.netwrote:
" sinbad" < sinbad.sin ... @ gmail.com写信息
新闻:76 ********************* ************* @ x19g2000 prg.googlegroups.com ...
"sinbad" <sinbad.sin...@gmail.comwrote in message
news:76**********************************@x19g2000 prg.googlegroups.com...
hi,
how连接散列定义的恒定值到另一个哈希
定义常量字符串。例如
how to concatenate a "hash defined" constant value to another "hash
defined" constant string. For example
#define ABC 100
#define ABC 100
#define MYSTR" ; ABC的价值是
#define MYSTR "The value of ABC is"
现在我需要一个将ABC的值连接到MYSTR的字符串。
我在编译时需要这个。
Now i need a string that will concatenate the value of ABC to MYSTR .
I need this at compile time.
结果常量字符串应为ABC的值为100。
The resultant constant string should be "The value of ABC is 100"
我该怎么做?
How can i do this.
您可以这样做:
__________________________________________________ ___________________
#include< stdio.h>
#define QUOTE_X(t)#t
#define QUOTE(t)QUOTE_X(t)
#define ABC 100
#define MYSTR" ABC的价值是
int main(){
* char const concat [] = MYSTR " QUOTE(ABC);
* printf("%s \ n",concat);
/ * -------- -------------------------------------------------- - * /
* put(" \\\\ _______________________ \
___________________________________ \ npress< ENTER to exit ...");
* getchar();
*返回0;
}
__________________________________________________ ___________________
You can do something like:
__________________________________________________ ___________________
#include <stdio.h>
#define QUOTE_X(t)#t
#define QUOTE(t)QUOTE_X(t)
#define ABC 100
#define MYSTR "The value of ABC is"
int main() {
* char const concat[] = MYSTR " " QUOTE(ABC);
* printf("%s\n", concat);
/*------------------------------------------------------------*/
* puts("\n\n_______________________\
___________________________________\npress <ENTERto exit...");
* getchar();
* return 0;
}
__________________________________________________ ___________________
chris,
但我需要在编译时这样做,因为我不能使用代码
你用main()函数写的。
因为我只会在执行期间得到结果字符串。
谢谢
sinbad
chris,
But i need to do this at compile time, meaing i can''t use the code
u''ve written in main () function.
Because i will get the resultant string only during execution time.
thanks
sinbad
这篇关于使用宏将常量连接到常量字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!