C語言條件編譯詳解

來源:互聯網
上載者:User

標籤:

預先處理程式提供了條件編譯的功能。可以按不同的條件去編譯不同的程式部分,因而產生不同的目標代碼檔案。這對於程式的移植和調試是很有用的。條件編譯有三種形式,下面分別介紹。

第一種形式

第一種形式的格式為:
    #ifdef  標識符
        程式段1
    #else
        程式段2
    #endif

它的功能是,如果標識符已被 #define命令定義過則對程式段1進行編譯;否則對程式段2進行編譯。如果沒有程式段2(它為空白),本格式中的#else可以沒有,即可以寫為:
    #ifdef  標識符
        程式段
    #endif

【例9-12】

  1. #include <stdio.h>
  2. #define NUM ok
  3. int main(void){
  4. struct stu{
  5. int num;
  6. char *name;
  7. char sex;
  8. float score;
  9. } *ps;
  10. ps=(struct stu*)malloc(sizeof(struct stu));
  11. ps->num=102;
  12. ps->name="Zhang ping";
  13. ps->sex=‘M‘;
  14. ps->score=62.5;
  15. #ifdef NUM
  16. printf("Number=%d\nScore=%f\n",ps->num,ps->score);
  17. #else
  18. printf("Name=%s\nSex=%c\n",ps->name,ps->sex);
  19. #endif
  20. free(ps);
  21. return 0;
  22. }

由於在程式的第14行插入了條件編譯預先處理命令,因此要根據NUM是否被定義過來決定編譯那一個printf語句。而在程式的第一行已對NUM作過宏定義,因此應對第一個printf語句作編譯故運行結果是輸出了學號和成績。

在程式的第1行宏定義中,定義NUM表示字串OK,其實也可以為任何字串,甚至不給出任何字串,寫為:

  1. #define NUM

也具有同樣的意義。只有取消程式的第1行才會去編譯第二個printf語句。讀者可上機試作。

第二種形式

第二種形式的格式為:
    #ifndef 標識符
        程式段1 
    #else 
        程式段2 
    #endif
與第一種形式的區別是將“ifdef”改為“ifndef”。它的功能是,如果標識符未被#define命令定義過則對程式段1進行編譯,否則對程式段2進行編譯。這與第一種形式的功能正相反。


http://www.zbnews.net/jiankang/list1/234895.html
http://www.zbnews.net/jiankang/list1/234894.html
http://www.zbnews.net/jiankang/list1/234891.html
http://www.51etong.com/jkzt/list1/234889.html
http://www.zbnews.net/jiankang/list1/234887.html
http://www.51etong.com/jkzt/list1/234886.html
http://www.zbnews.net/jiankang/list1/234885.html
http://www.zbnews.net/jiankang/list1/234884.html
http://www.zbnews.net/jiankang/list1/234882.html
http://www.51etong.com/jkzt/list1/234880.html
http://www.lznews.gov.cn/uzt/list1/234879.html
http://www.lznews.gov.cn/uzt/list1/234878.html
http://www.51etong.com/jkzt/list1/234876.html
http://www.lznews.gov.cn/uzt/list1/234875.html
http://www.lznews.gov.cn/uzt/list1/234874.html
http://www.lznews.gov.cn/uzt/list1/234873.html
http://www.zbnews.net/jiankang/list1/234872.html
http://www.51etong.com/jkzt/list1/234871.html
http://www.51etong.com/jkzt/list1/234870.html
http://www.51etong.com/jkzt/list1/234867.html
http://user.qzone.qq.com/2633883405
http://t.qq.com/changchuny8250
http://health.people.com.cn/xywy/wfyxb/tp/8634716732.html
http://health.people.com.cn/xywy/wfyxb/tp/8634711498.html
http://health.people.com.cn/xywy/wfyxb/tp/8634708130.html
http://health.people.com.cn/xywy/wfyxb/tp/8634705327.html
http://health.people.com.cn/xywy/wfyxb/tp/8634702557.html
http://health.people.com.cn/xywy/wfyxb/tp/8634699443.html
http://health.people.com.cn/xywy/wfyxb/tp/8634695041.html
http://health.people.com.cn/xywy/wfyxb/tp/8634692366.html
http://health.people.com.cn/xywy/wfyxb/tp/8634689457.html
http://health.people.com.cn/xywy/wfyxb/tp/8634686655.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8634680004.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8634675843.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8634671613.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8634668953.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8634665768.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8634662151.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8634659390.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8634655387.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8634652223.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8634648791.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8634645687.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8634642791.html
http://health.people.com.cn/xywy/wfyxb/zl/8634639673.html
http://health.people.com.cn/xywy/wfyxb/zl/8634636341.html
http://health.people.com.cn/xywy/wfyxb/zl/8634633176.html
http://health.people.com.cn/xywy/wfyxb/zl/8634629876.html
http://health.people.com.cn/xywy/wfyxb/zl/8634626116.html
http://health.people.com.cn/xywy/wfyxb/zl/8634621396.html
http://health.people.com.cn/xywy/wfyxb/zl/8634618202.html
http://health.people.com.cn/xywy/wfyxb/zl/8634615071.html
http://health.people.com.cn/xywy/wfyxb/zl/8634612138.html
http://health.people.com.cn/xywy/wfyxb/kfal/8634608141.html
http://health.people.com.cn/xywy/wfyxb/kfal/8634601130.html
http://health.people.com.cn/xywy/wfyxb/kfal/8634598198.html
http://health.people.com.cn/xywy/wfyxb/kfal/8634595171.html
http://health.people.com.cn/xywy/wfyxb/kfal/8634591848.html
http://health.people.com.cn/xywy/wfyxb/kfal/8634587855.html
http://health.people.com.cn/xywy/wfyxb/kfal/8634584673.html
http://health.people.com.cn/xywy/wfyxb/kfal/8634580296.html
http://health.people.com.cn/xywy/wfyxb/kfal/8634576927.html
http://health.people.com.cn/xywy/wfyxb/kfal/8634572513.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8634568629.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8634565548.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8634561012.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8634558160.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8634555287.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8634551027.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8634546991.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8634543966.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8634540202.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8665825627.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8665803242.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8665760857.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8665738541.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8665725796.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8665705278.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8665676307.html
http://health.people.com.cn/xywy/wfyxb/tp/8665652613.html
http://health.people.com.cn/xywy/wfyxb/tp/8665634968.html
http://health.people.com.cn/xywy/wfyxb/tp/8665618204.html
http://health.people.com.cn/xywy/wfyxb/tp/8665599559.html
http://health.people.com.cn/xywy/wfyxb/tp/8665570637.html
http://health.people.com.cn/xywy/wfyxb/tp/8665546616.html
http://health.people.com.cn/xywy/wfyxb/tp/8665516148.html
http://health.people.com.cn/xywy/wfyxb/tp/8665494052.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8665348011.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8665332141.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8665304778.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8665279337.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8665245471.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8665217641.html
http://health.people.com.cn/xywy/wfyxb/zl/8665183707.html
http://health.people.com.cn/xywy/wfyxb/zl/8665151882.html
http://health.people.com.cn/xywy/wfyxb/zl/8665126913.html
http://health.people.com.cn/xywy/wfyxb/zl/8665095276.html
http://health.people.com.cn/xywy/wfyxb/zl/8665065223.html
http://health.people.com.cn/xywy/wfyxb/zl/8665044459.html
http://health.people.com.cn/xywy/wfyxb/zl/8665016966.html
http://health.people.com.cn/xywy/wfyxb/zl/8664994329.html
http://health.people.com.cn/xywy/wfyxb/zl/8664964107.html
http://health.people.com.cn/xywy/wfyxb/zl/8664946776.html
http://health.people.com.cn/xywy/wfyxb/kfal/8664904496.html
http://health.people.com.cn/xywy/wfyxb/kfal/8664875723.html
http://health.people.com.cn/xywy/wfyxb/kfal/8664841175.html
http://health.people.com.cn/xywy/wfyxb/kfal/8664820153.html
http://health.people.com.cn/xywy/wfyxb/kfal/8664800127.html
http://health.people.com.cn/xywy/wfyxb/kfal/8664786867.html
http://health.people.com.cn/xywy/wfyxb/kfal/8664757276.html
http://health.people.com.cn/xywy/wfyxb/kfal/8664738646.html
http://health.people.com.cn/xywy/wfyxb/kfal/8664718323.html
http://health.people.com.cn/xywy/wfyxb/kfal/8664688557.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8664668308.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8664646362.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8664631785.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8664603483.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8664580771.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8664560208.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8664545675.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8664532567.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8664505390.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8664484808.html

第三種形式

第三種形式的格式為:
    #if 常量運算式
        程式段1
    #else 
        程式段2
    #endif
它的功能是,如常量運算式的值為真(非0),則對程式段1 進行編譯,否則對程式段2進行編譯。因此可以使程式在不同條件下,完成不同的功能。

【例9-13】

  1. #include <stdio.h>
  2. #define R 1
  3. int main(void){
  4. float c,r,s;
  5. printf ("input a number: ");
  6. scanf("%f",&c);
  7. #if R
  8. r=3.14159*c*c;
  9. printf("area of round is: %f\n",r);
  10. #else
  11. s=c*c;
  12. printf("area of square is: %f\n",s);
  13. #endif
  14. return 0;
  15. }

本例中採用了第三種形式的條件編譯。在程式第1行宏定義中,定義R為1,因此在條件編譯時間,常量運算式的值為真,故計算並輸出圓面積。

C語言條件編譯詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.