Free mind

          Be fresh and eager every morning, and tired and satisfied every night.
          posts - 39, comments - 2, trackbacks - 0, articles - 0
             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          預處理器

          Posted on 2007-05-30 09:56 morphis 閱讀(246) 評論(0)  編輯  收藏 所屬分類: 1. C/Cpp

          預處理器

           

          預處理器在編譯器之前根據(jù)指令更改程序文本。編譯器看到的是預處理器修改的代文本(稱作“翻譯單元(translation unit)”)。

           

          預處理器的三大功能:

          1。文件或源文件包含;

          2。宏展;

          3。條件編譯;

           

          文件包含:

          #include <stdio.h>         //編譯器首先到文件庫查找;

          #include “myHead.h”     //編譯器首先到當前目找,如找不到再到文件庫查找;

           

          預處理指令:

          #include  #define  #undef  #if  #elif  #else  #endif  #ifdef  #ifndef  #error  #line  #pragma

           

          宏定

          #define一個宏;用#undef除一個宏;

           

          宏的分

          無參數(shù)宏(類對象宏)

          例:

          #define MAXLINES 500

          類對象宏似常數(shù),因此可以用下面句代替,并且推薦用下面句來定。

          Const int MAXLINES = 500;

           

                 參數(shù)宏(函數(shù)宏)

          例:

          #define abs(x)  ( (x) > 0 ? (x) : (- (x) ) )          

          //參數(shù)最好用括號封裝,這樣在替換時不會產(chǎn)生歧

          函數(shù)宏大多可以用內(nèi)聯(lián)函數(shù)來代替,并且推薦。

          inline int abs(int x)

          {

                 return x > 0 ? x : (-x);

          }

           

          宏:

          __LINE__  __FILE__  __DATE__  __TIME__  __cplusplus

           

          例:

          // sysmac.cpp

          #include <iostram>

          using namespace std;

          main()

          {

                 Cout << “__DATE__ == “ << __DATE__ << endl;

                 Cout << “__TIME__ == “ << __TIME__ << endl;

                 Cout << “__LINE__ == “ << __LINE__ << endl;

                 Cout << “__FILE__ == “ << __FILE__ << endl;

          }

          //出:

          __DATE__ == Nov 28 1996

          __TIME__ == 09:37:38

          __LINE__ == 7

          __FILE__ == sysmac.cpp

           

          預處理運算符:

          #  ##  Defined

          # 字符串化;## 標記;

          具體看下面的例子:

          #include <stdio.h>

           

          #define trace(x,format)  printf(#x  “= %”  #format  “\n” , x)

          #define trace2(i)  trace(x  ## i , d)

           

          main()

          {

                 int i = 1;

          float x = 2.0;

          char *s = “three”;

           

          trace(i,d);                     //printf( “x” “=%” “d” “\n” , x)

          trace(x,f);

          trace(s,s);

           

          Int x1=1,x2=2,x3=3;

           

          trace2(1);                     //trace(x1,d)

          trace2(2);

          trace2(3);

          }

          //出:

          i = 1

          x = 2.000000

          s = three

          x1 = 1

          x2 = 2

          x3 = 3

           

          主站蜘蛛池模板: 襄垣县| 宝山区| 台中市| 河北省| 莲花县| 玉门市| 闽清县| 广安市| 平罗县| 辽源市| 醴陵市| 青海省| 丰原市| 麦盖提县| 招远市| 泰宁县| 安达市| 五家渠市| 陈巴尔虎旗| 当阳市| 无为县| 右玉县| 武汉市| 辉县市| 长沙县| 北川| 仁化县| 中宁县| 固镇县| 泸溪县| 拜城县| 万全县| 永平县| 崇明县| 大冶市| 漯河市| 太和县| 陈巴尔虎旗| 余干县| 阜阳市| 东乌珠穆沁旗|