m***t 发帖数: 254 | 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? | m***t 发帖数: 254 | 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;
} | O*******d 发帖数: 20343 | 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. |
|