The meaning of # ## in C++/C macro definition (define)

Original link: http://blog.csdn.net/qq_15457239/article/details/56842450


#define f(a,b) a##b 
#define d(a) #a 
#define s(a) d(a) 

void main( void ) 

    puts(d(f(a,b))); 
    puts(s(f(a,b))); 


输出结果: 
f(a,b) 
ab

Analysis: ##Connecting two symbols 
    #a means treating a as a symbol, that is, treating the following # as a string


The # and ## operators are used with the #define macro. Use # to return the first argument after # as a quoted string. For example, the command 
    #define to_string( s ) # s 
will cause the compiler to The following command 
    cout < < to_string( Hello World! ) < < endl; 
is understood as 
    cout < < "Hello World!" < < endl; 
Use ## to concatenate the content before and after ##. For example, the command 
    #define concatenate( x, y ) x ## y 
    ... 
    int xy = 10; 
    ...  will
cause the compiler to interpret 
    cout < < concatenate( x, y ) < < endl; 
as 
    cout < < xy < < endl; 
'10' is displayed at the output.

 

puts(d(f(a,b)));  ----> 因为d宏中的参数是另外一个宏,且带##,所以作为参数的宏不展开,相当于 
                            puts(#f(a,b));----->puts("f(a,b)"); 
puts(s(f(a,b))); ----> 因为s宏中的参数是另外一个宏,但不带##,所以作为参数的宏先展开,相当于 
                            puts(s(ab));----->puts(d(ab));---->puts(#ab);---->puts("ab");

 

#define f(a,b) a##b 
#define d(a) #a --》 以"#"开头的,直接替换,不展开:immediately replaced by the unexpanded actual argument 
#define s(a) d(a) --》 非以"#"开头的,先展开,再替换,也就是一般的情况 

所以就两种情况: 
1,不以"#"开头的,先展开参数a,然后是替换代码:puts(s(f(a,b)));-->puts(s(ab))-->puts(d(ab))-->puts("ab") 
2,以"#"开头的,直接替换,不展开:puts(d(f(a,b)))-->puts("f(a,b)")

原文链接:http://blog.csdn.net/qq_15457239/article/details/56842450


#define f(a,b) a##b 
#define d(a) #a 
#define s(a) d(a) 

void main( void ) 

    puts(d(f(a,b))); 
    puts(s(f(a,b))); 


输出结果: 
f(a,b) 
ab

分析:  ##把两个符号连起来 
    #a指把a当成符号,就是把#后面的看成字符串


# 和 ## 操作符是和#define宏使用的. 使用# 使在#后的首个参数返回为一个带引号的字符串. 例如, 命令 
    #define to_string( s ) # s 
将会使编译器把以下命令 
    cout < < to_string( Hello World! ) < < endl; 
理解为 
    cout < < "Hello World!" < < endl; 
使用##连结##前后的内容. 例如, 命令 
    #define concatenate( x, y ) x ## y 
    ... 
    int xy = 10; 
    ... 
将会使编译器把 
    cout < < concatenate( x, y ) < < endl; 
解释为 
    cout < < xy < < endl; 
理所当然,将会在标准输出处显示'10'.

 

puts(d(f(a,b)));  ----> 因为d宏中的参数是另外一个宏,且带##,所以作为参数的宏不展开,相当于 
                            puts(#f(a,b));----->puts("f(a,b)"); 
puts(s(f(a,b))); ----> 因为s宏中的参数是另外一个宏,但不带##,所以作为参数的宏先展开,相当于 
                            puts(s(ab));----->puts(d(ab));---->puts(#ab);---->puts("ab");

 

#define f(a,b) a##b 
#define d(a) #a --》 以"#"开头的,直接替换,不展开:immediately replaced by the unexpanded actual argument 
#define s(a) d(a) --》 非以"#"开头的,先展开,再替换,也就是一般的情况 

所以就两种情况: 
1,不以"#"开头的,先展开参数a,然后是替换代码:puts(s(f(a,b)));-->puts(s(ab))-->puts(d(ab))-->puts("ab") 
2,以"#"开头的,直接替换,不展开:puts(d(f(a,b)))-->puts("f(a,b)")

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325936081&siteId=291194637