c - 为什么预处理器的标记粘贴运算符 ## 对变量不起作用?

标签 c variables c-preprocessor

#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,它会完美地工作。错误是 ab函数中未声明的首次使用。

最佳答案

预处理器在编译时运行。所以 '##' 运算符在编译时被评估。您不能将它与运行时确定的值一起使用。

这里发生的是 a##b 被简单地转换为 ab,因此错误 ab undefined

关于c - 为什么预处理器的标记粘贴运算符 ## 对变量不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58582609/

相关文章:

仅在 visual studio 2012 中为 C 编译选项

c - 有人知道 atolf c 函数吗?

c++ - 如何在 OpenCv 中将 cv::Mat 转换为灰度?

javascript - JS if 语句无法正常工作

c# - 将 C 定义宏转换为 C#

C++ 预处理器在类关键字之后和类名之前定义

c - 使用 qsort 和 void 指针问题对结构进行排序

c - Makefile链接错误: undefined main

javascript - 变量在我的函数中变得未定义

c - 参数化包括