2

我无法理解预处理器是如何工作的以及##在这个特定示例中代表什么

#include <stdio.h>

#define TEMP_KEY(type,Key) (TEMP_##type | Key)

enum TEMPKey_Type
{
    TEMP_UNKNOWN = 0,
    TEMP_SPECIAL ,
    TEMP_UNICODE
};

enum Actual_Key
{
    TEMP_RIGHT = TEMP_KEY(UNKNOWN,0x1),
    TEMP_LEFT = TEMP_KEY(SPECIAL,0x1),
    TEMP_UP = TEMP_KEY(UNICODE,0x1)
};

int main()
{
    printf("\n Value of TEMP_RIGHT : %d ",TEMP_RIGHT);
    printf("\n Value of TEMP_LEFT : %d ",TEMP_LEFT);
    printf("\n Value of TEMP_UP : %d ",TEMP_UP);

    return 0;
}

这是如何 #define TEMP_KEY(type,Key) (TEMP_##type | Key) 工作的,或者在预处理过程中如何以及究竟TEMP_##type替换了什么?

4

2 回答 2

4

“##”表示连接。因此TEMP_RIGHT = TEMP_KEY(UNKNOWN,0x1)变为TEMP_RIGHT = TEMP_UNKNOWN | 0x1,(“TEMP_”和“UNKNOWN”连接在一起)

于 2011-02-16T15:43:12.063 回答
2

##是#define 指令中的连接运算符。

例如,TEMP_KEY(UNICODE,0x1) 调用的 TEMP_##type 生成下一个代码:

(TEMP_UNICODE | 0x1)
于 2011-02-16T15:44:07.923 回答