1. 程式人生 > >#define巨集定義中的#和##用法和區別

#define巨集定義中的#和##用法和區別

參考Difference between # and ## 簡單來說,在巨集定義中,#是將變數轉換成字串的作用,而##是連線兩個引數作用

###exaplem

#include <iostream>

#define stringize_indirect( a ) stringize(a)
#define stringize( a ) #a
#define paste( a, b ) a ## b

int main()
{
    std::cout << stringize( strin ) stringize( gize ) << '\n' ;
    // std::cout << "strin" "gize" << '\n' ;

    std::cout << paste(1,2) << '\n';

    std::cout << stringize( paste( strin, gize ) ) << '\n' ;
    // std::cout << "paste( strin, gize )" << '\n' ;

    std::cout << stringize_indirect( paste( strin, gize ) ) << '\n' ;
    // std::cout << "stringize" << '\n' ;
}

執行結果

stringize
12
paste( strin, gize )
stringize