Skip to content

Instantly share code, notes, and snippets.

@kkdai
Created March 5, 2014 11:04
Show Gist options
  • Select an option

  • Save kkdai/9365213 to your computer and use it in GitHub Desktop.

Select an option

Save kkdai/9365213 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <stdio.h>
int main(int argc, const char * argv[])
{
#define PRINTX(x) std::cout << #x << " = " <<x <<"\n"
// ANSI C 比 K&R C 功能多了 # 及 ## ,這部份其實是 #2 的續篇
// 在 #2 中說到 ANSI 及 K&R 差異,現在再加上此部份,ANSI C 又
// 勝一籌!!
//
// 說明1:參數前有 # ,則用 " " 括住參數,若是參數中有 " 則加
// 上一 \ .
//
//EX:
//
// 原來寫法:
//
// puts("在 C: 下有一 \"TEST.COM\" 檔案");
//
// 輸出結果: 在 C: 下有一 "TEST.COM" 檔案
//
// 改用巨集:
//
//#define STR(X) #X
//
// puts(STR(在 C: 下有一 "TEST.COM" 檔案));
//
// 你看!! 是不是以後寫起來方便多了,且看起來清楚多了!!
//
// 說明2:## 會將前後兩語法單元連接
//
//EX:
//
//#define STR(X,Y) #X ## #Y
//
// puts(STR(This is "A",This is "B"));
//
// 輸出結果: This is "A"This is "B"
int A=5;
PRINTX(A);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment