[英]Why doesn't the preprocessor's token pasting operator ## work on variables?
#if !defined(STDIO_H) && !defined(STDLIB_H)
#include<stdio.h>
#include<stdlib.h>
#endif
#if !defined(LIMITS_H)
#include<limits.h>
#endif
#if !defined(MATH_H)
#define pow(a,b) {\
int i=1,p=1;\
while(i<=b)\
{\
p=p*a;\
i++;\
}\
printf("%d\n",p);\
}
#define join(a,b) a##b
#endif
int main()
{
int a,b;
printf("Enter a b : ");
scanf("%d %d",&a,&b);
pow(a,b);
printf("%d\n",join(a,b));
return 0;
}
錯誤在行printf("%d",join(a,b));
但如果我用 56 和 34 之類的常量替換 a 和 b,它會完美運行。錯誤是在 function 中未聲明的第一次使用。
預處理器在編譯時運行。 因此,'##' 運算符在編譯時進行評估。 您不能將它與在運行時確定的值一起使用。
這里發生的是a##b
被簡單地轉換為ab
,因此錯誤ab undefined
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.