开发者

PreProcessing #define

开发者 https://www.devze.com 2023-02-11 01:48 出处:网络
I am unable to understand how the preprocessor works and what does the ## stands for in this particular example

I am unable to understand how the preprocessor works and what does the ## stands for in this particular example

#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 T开发者_运维技巧EMP_RIGHT : %d ",TEMP_RIGHT);
    printf("\n Value of TEMP_LEFT : %d ",TEMP_LEFT);
    printf("\n Value of TEMP_UP : %d ",TEMP_UP);

    return 0;
}

How does this #define TEMP_KEY(type,Key) (TEMP_##type | Key) work or how and what exactly is TEMP_##type replaced by during preprocessing?


The "##" means concatenate. Therefore TEMP_RIGHT = TEMP_KEY(UNKNOWN,0x1) becomes TEMP_RIGHT = TEMP_UNKNOWN | 0x1, ("TEMP_" and "UNKNOWN" are joined together)


## is the concatenation operator in #define directives.

For example, TEMP_##type for TEMP_KEY(UNICODE,0x1) call generates next code:

(TEMP_UNICODE | 0x1)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号