C語言宏替換的幾種用法 【轉載】

出處:
http://topic.csdn.net/t/20030820/15/2168502.html

①簡單宏替換  
   
      #defind   Pi   3.14159  
  或  
        #ifndef   __THIS_FILE__  
        #define   __THIS_FILE__     //   用於防止重複包含文件  
            ……   ……  
        #endif  
  ___________________________  
   
  ②條件宏替換  
   
      #define   p(x)   printf(x)  
      ……   ……  
        p("hello!");  
   
  →   hello!  
  ___________________________  
   
  ③字符宏替換(#@)  
   
        #define   pchar(x)     printf("%c\n",   #@x)  
      ……   ……  
        pchar(a);  
   
  →   a  
  ___________________________  
   
  ④字串宏替換(#)  
   
        #define   pstring(x)     printf("%s\n",   #x)  
      ……   ……  
        pstring(hello!);  
   
  →   hello!  
  ___________________________  
   
  ⑤連接宏替換(##)  
   
        #define   p(   n   )   printf(   "symbol"   #n   "   =   %d",   symbol##n   )  
            ……   ……  
        int   symbol9   =   9;  
        p(   9   );  
   
  →   symbol9=9
C語言宏收藏

ANSI C標準中有幾個標準預定義宏:__FILE__     __DATE__   __TIME___    __LINE__   等

__LINE__:在源代碼中插入當前源代碼行號;

__FILE__:在源文件中插入當前源文件名;

__DATE__:在源文件中插入當前的編譯日期

__TIME__:在源文件中插入當前編譯時間;

__STDC__:當要求程序嚴格遵循ANSI C標準時該標識被賦值爲1;

__cplusplus:當編寫C++程序時該標識符被定義。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章