s*****n 发帖数: 461 | 1 为什么line1合法,line2非法?
#include
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)
int main(void)
{
printf("\n%s", g(f((1,2),3)) ) ; /*line1*/
printf("\n%s", h(f((1,2),3)) ) ; /*line2*/
} | X****r 发帖数: 3557 | 2 宏函数替换的时候所有不是用在#或##里的参数也会被宏展开,所以你这里g和h
并不等价。
g(f((1,2),3)) => "f((1,2),3))"
h(f((1,2),3)) => g(f((1,2),3)的宏展开),
但是f((1,2),3) => (1,2)3 编译错误
C89 6.10.3.1 Argument substitution
1 After the arguments for the invocation of a function-like
macro have been identified, argument substitution takes
place. A parameter in the replacement list, unless
preceded by a # or ## preprocessing token or followed by
a ## preprocessing token (see below), is replaced by the
corresponding argument after all
【在 s*****n 的大作中提到】 : 为什么line1合法,line2非法? : #include : #define f(a,b) a##b : #define g(a) #a : #define h(a) g(a) : int main(void) : { : printf("\n%s", g(f((1,2),3)) ) ; /*line1*/ : printf("\n%s", h(f((1,2),3)) ) ; /*line2*/ : }
| s*****n 发帖数: 461 | |
|