Redian新闻
>
Question on using ## in #define
avatar
Question on using ## in #define# Programming - 葵花宝典
m*t
1
#include
#define REPLACE hello world
#define JOIN(a, b) a##b
int main() {
printf("JOIN(REP, LACE)\n");
return 0;
}
run on gcc, output:
JOIN(REP, LACE)
Why?
avatar
m*t
2
Thanks. That works. But i am kind of curious since I thought preprocessor
should handle the string substitution. And i tried the following which gives
me compilation error.
#include
#define REPLACE hello world
#define JOIN(a, b) a##b
int main() {
printf("%s", JOIN(REP, LACE));
return 0;
}
avatar
O*d
3
C preprocessor will not replace literal string. C string inside double quote
is literal except escaped sequence. In your first case, "JOIN(REP, LACE)\n
" is a literal string "JOIN(REP, LACE)" followed by a new line character. In
your second case, hello world is not a string.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。