#ifndef、#def、#endif等宏的含义
Ted
posted @ 2008年8月12日 17:57
in
未分类
with tags
ifdef c
, 2774 阅读
|
|
|
一般情况下,源程序中所有的行都参加编译。但是有时希望对其中一部分内容只在满足一定条件才进行编译,也就是对一部分内容指定编译的条件,这就是“条件编译”。有时,希望当满足某条件时对一组语句进行编译,而当条件不满足时则编译另一组语句。
条件编译命令最常见的形式为:
#ifdef 标识符
程序段1
#else
程序段2
#endif
它的作用是:当标识符已经被定义过(一般是用#define命令定义),则对程序段1进行编译,否则编译程序段2。
其中#else部分也可以没有,即:
#ifdef
程序段1
#endif
这里的“程序段”可以是语句组,也可以是命令行。这种条件编译可以提高C源程序的通用性。如果一个C源程序在不同计算机系统上系统上运行,而不同的计算机 又有一定的差异。例如,我们有一个数据类型,在Windows平台中,应该使用long类型表示,而在其他平台应该使用float表示,这样往往需要对源 程序作必要的修改,这就降低了程序的通用性。可以用以下的条件编译:
#ifdef WINDOWS
#define MYTYPE long
#else
#define MYTYPE float
#endif
如果在Windows上编译程序,则可以在程序的开始加上
#define WINDOWS
这样则编译下面的命令行:
#define MYTYPE long
如果在这组条件编译命令之前曾出现以下命令行:
#define WINDOWS 0
则预编译后程序中的MYTYPE都用float代替。这样,源程序可以不必作任何修改就可以用于不同类型的计算机系统。当然以上介绍的只是一种简单的情况,可以根据此思路设计出其它的条件编译。
例如,在调试程序时,常常希望输出一些所需的信息,而在调试完成后不再输出这些信息。可以在源程序中插入以下的条件编译段:
#ifdef DEBUG
print ("device_open(%p)\n", file);
#endif
如果在它的前面有以下命令行:
#define DEBUG
则在程序运行时输出file指针的值,以便调试分析。调试完成后只需将这个define命令行删除即可。有人可能觉得不用条件编译也可达此目的,即在调试 时加一批printf语句,调试后一一将printf语句删除去。的确,这是可以的。但是,当调试时加的printf语句比较多时,修改的工作量是很大 的。用条件编译,则不必一一删改printf语句,只需删除前面的一条“#define DEBUG”命令即可,这时所有的用DEBUG作标识符的条件编译段都使其中的printf语句不起作用,即起统一控制的作用,如同一个“开关”一样。
有时也采用下面的形式:
#ifndef 标识符
程序段1
#else
程序段2
#endif
只是第一行与第一种形式不同:将“ifdef”改为“ifndef”。它的作用是:若标识符未被定义则编译程序段1,否则编译程序段2。这种形式与第一种形式的作用相反。
以上两种形式用法差不多,根据需要任选一种,视方便而定。
还有一种形式,就是#if后面的是一个表达式,而不是一个简单的标识符:
#if 表达式
程序段1
#else
程序段2
#endif
它的作用是:当指定的表达式值为真(非零)时就编译程序段1,否则编译程序段2。可以事先给定一定条件,使程序在不同的条件下执行不同的功能。
例如:输入一行字母字符,根据需要设置条件编译,使之能将字母全改为大写输出,或全改为小写字母输出。
#define LETTER 1
main()
{
char str[20]="C Language",c;
int i=0;
while((c=str[i])!='\0'){
i++;
#if LETTER
if(c>='a'&&c<='z') c=c-32;
#else
if(c>='A'&&c<='Z') c=c+32;
#endif
printf("%c",c);
}
}
运行结果为:C LANGUAGE
现在先定义LETTER为1,这样在预处理条件编译命令时,由于LETTER为真(非零),则对第一个if语句进行编译,运行时使小写字母变大写。如果将程序第一行改为:
#define LETTER 0
则在预处理时,对第二个if语句进行编译处理,使大写字母变成小写字母(大写字母与相应的小写字母的ASCII代码差32)。此时运行情况为:
c language
有人会问:不用条件编译命令而直接用if语句也能达到要求,用条件编译命令有什么好处呢?的确,此问题完全可以不用条件编译处理,但那样做目标程序长(因 为所有语句都编译),而采用条件编译,可以减少被编译的语句,从而减少目标的长度。当条件编译段比较多时,目标程序长度可以大大减少。
|
|
2020年12月07日 01:02
This is a good post. This post gives truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. Thank you so much. Keep up the good works chymall login
2020年12月07日 19:53
Please share more like that. 123 movie
2020年12月10日 22:20
This is my first time visit here. From the tons of comments on your articles,I guess I am not only one having all the enjoyment right here! Kyocera router password
2020年12月24日 15:44 Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include. casino online canada
2020年12月29日 02:29
Our products are of the highest quality and are packed in discrete odor less vacuum sealed packages. Discreet delivery is our recommended best practice and packages are tracked all the way to your home.
<a href="https://exoticbudshop.org/" rel="dofollow">Buddha bear carts</a>
<a href="https://exoticbudshop.org/product/buy-buddha-bear-carts/" rel="dofollow">Buddha bear carts</a> at prices that just can not be beaten.
Following the footsteps of plenty of other countries and states that have realized the health benefits of medicating with cannabis. We believe patients should have access to the best quality.
<a href="https://exoticbudshop.org/product/buy-buddha-bear-carts/" rel="dofollow">buddha bear carts for sale</a> Free Shipping on orders above $200
<a href="https://exoticbudshop.org/product/buy-buddha-bear-carts/" rel="dofollow">Buddah bear carts</a>
<a href="https://exoticbudshop.org/product/buy-buddha-bear-carts/" rel="dofollow">buddha bear carts</a>
<a href="https://exoticbudshop.org/product/buy-krt-carts-online/" rel="dofollow">KRT carts</a>
<a href="https://exoticbudshop.org/product/buy-krt-carts-online/" rel="dofollow">Buy KRT carts</a> Buy quality THC VAPE AT EXOTICBUDSHOP and have it deliver to you discretely
<a href="https://exoticbudshop.org/product/buy-biscotti-boyz-tins-weed/" rel="dofollow">biscottiboyz</a> > Discreet delivery is our recommended best practice and packages are tracked all the way to your home.
<a href="https://exoticbudshop.org/product/buy-lucky-carts-online/" rel="dofollow">lucky carts</a> Our products are of great quality
<a href="https://exoticbudshop.org/product/buy-lucky-carts-online/" rel="dofollow">buy lucky carts</a>
<a href="https://exoticbudshop.org/product/muha-meds-carts-for-sale/" rel="dofollow">muhameds carts</a> Our products are of great quality
<a href="https://exoticbudshop.org/product/muha-meds-carts-for-sale/" rel="dofollow">buy muhameds carts</a>
<a href="https://exoticbudshop.org/product/muha-meds-carts-for-sale/" rel="dofollow">buy muhameds carts online</a> Our products are of great quality
2021年4月10日 20:01
hese websites ensure that they collect information from several reliable sources rather than from middlemen and scammers.
2021年4月19日 01:11
With the benefits you get in on the web gaming activities, you will be happy and pleased playing with a feeling of ease and protection, and you will not hesitate to register for on line slots.
2021年5月27日 20:31
I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
2021年5月30日 18:11
I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!
2021年7月13日 20:35
I really loved reading your blog. It was very well authored and easy to undertand. Unlike additional blogs I have read which are really not tht good. I also found your posts very interesting. In fact after reading. I had to go show it to my friend and he ejoyed it as well! Modern Dog Magazine
2021年7月18日 18:19
The net will be bogged straight down together with fake sites without genuine concept nevertheless the submit has been great and also worth the particular examine. Many thanks regarding revealing this kind of with me at night. face mask
2021年7月29日 20:01
Discount put up, Regards for the purpose of showing This unique practical knowledge. Excellently crafted piece of writing, if perhaps virtually all web guru proposed an identical standard of articles and other content whenever you, the on-line world will be more effective destination. Why not keep writing! walmartone wire login
2021年9月10日 01:29
Hindiyojana.in is a dedicated portal to share detailed information regarding various government schemes. We cover central govt's and state wise schemes. Be advised that this is not official website of any scheme and is not linked with any Indian govt's ministry. Indian Government Schemes
2022年1月09日 22:45
For all right reasons I will rat ethis site called signnow.com/ask/how-does-the-esign-act-recognize-electronic-contracts-as-equivalent-to-written-contracts 5/5. The reason is the quality of service which they provide is amazing. No cons at all!
2022年1月13日 19:17
You are travel lovers? Where will you go for the next destination? Let Things to do post show you their favorite places with their beautiful photos.
2022年3月19日 23:45
I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article... https://www.posterprintcenter.com/canvas-prints.html
2022年9月26日 06:03
Great stuff. i am going to bookmark it, if you are looking for bluetooth fm transmitter, xiaomi redmi airdots, wifi versterker, beste wifi versterker, nokia 105, smartphone holder in Netherlands, get in touch with us.