C语言宏替换的几种用法 【转载】

来源:互联网 发布:航天开盘软件 编辑:程序博客网 时间:2024/06/07 05:19
出处:
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语言宏收藏
http://blog.csdn.net/tony821224/archive/2008/02/25/2119340.aspx

ANSI C标准中有几个标准预定义宏:__FILE__     __DATE__   __TIME___    __LINE__   等

__LINE__:在源代码中插入当前源代码行号;

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

__DATE__:在源文件中插入当前的编译日期

__TIME__:在源文件中插入当前编译时间;

__STDC__:当要求程序严格遵循ANSI C标准时该标识被赋值为1;

__cplusplus:当编写C++程序时该标识符被定义。