標籤:
預先處理程式提供了條件編譯的功能。可以按不同的條件去編譯不同的程式部分,因而產生不同的目標代碼檔案。這對於程式的移植和調試是很有用的。條件編譯有三種形式,下面分別介紹。
第一種形式
第一種形式的格式為:
#ifdef 標識符
程式段1
#else
程式段2
#endif
它的功能是,如果標識符已被 #define命令定義過則對程式段1進行編譯;否則對程式段2進行編譯。如果沒有程式段2(它為空白),本格式中的#else可以沒有,即可以寫為:
#ifdef 標識符
程式段
#endif
【例9-12】
- #include <stdio.h>
- #define NUM ok
- int main(void){
- struct stu{
- int num;
- char *name;
- char sex;
- float score;
- } *ps;
- ps=(struct stu*)malloc(sizeof(struct stu));
- ps->num=102;
- ps->name="Zhang ping";
- ps->sex=‘M‘;
- ps->score=62.5;
- #ifdef NUM
- printf("Number=%d\nScore=%f\n",ps->num,ps->score);
- #else
- printf("Name=%s\nSex=%c\n",ps->name,ps->sex);
- #endif
- free(ps);
- return 0;
- }
由於在程式的第14行插入了條件編譯預先處理命令,因此要根據NUM是否被定義過來決定編譯那一個printf語句。而在程式的第一行已對NUM作過宏定義,因此應對第一個printf語句作編譯故運行結果是輸出了學號和成績。
在程式的第1行宏定義中,定義NUM表示字串OK,其實也可以為任何字串,甚至不給出任何字串,寫為:
- #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】
- #include <stdio.h>
- #define R 1
- int main(void){
- float c,r,s;
- printf ("input a number: ");
- scanf("%f",&c);
- #if R
- r=3.14159*c*c;
- printf("area of round is: %f\n",r);
- #else
- s=c*c;
- printf("area of square is: %f\n",s);
- #endif
- return 0;
- }
本例中採用了第三種形式的條件編譯。在程式第1行宏定義中,定義R為1,因此在條件編譯時間,常量運算式的值為真,故計算並輸出圓面積。
C語言條件編譯詳解