宏定义中"#"的用法

    技术2022-06-24  63

    惭愧, 今天测试了才真正明白, 在宏定义中使用"#",  还是因为群里有TX提出, 才测试的

     

    看代码:

     

    #define macro(a) #a #define macro2(a,b) a##b #define macro3(a,b,c) a##b##c

     

    #a,  表示a不再是一个变量, 而变成了字符串"a"

    ##表示连接, a##b,  表示输入的参数名为ab,  a##b##c同理, 代表变量名为: abc

     

    测试例子:

    int x = 3; int y = 4; int xy = 10; int xyz=20; CString str; OutputDebugString(macro(x)); str.Format("%d",macro2(x,y)); OutputDebugString(str); str.Format("%d",macro3(x,y,z)); OutputDebugString(str);

     

    输出结果为:

    x

    10

    20

     

    第一个为x,  marco(x),  x变成了"x"字符串

    第二个为10, macro(x,y), 就是变量xy

    第三个为20, macro(x,y,z), 就是变量xyz

     

     


    最新回复(0)