<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[预处理-条件编译]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Thu, 04 Dec 2008 07:43:57 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	预处理程序提供了条件编译的功能。 可以按不同的条件去编译不同的程序部分，因而产生不同的目标代码文件。 这对于程序的移植和调试是很有用的。<br/><br/>条件编译有三种形式，下面分别介绍：<br/>1. 第一种形式：<br/>#ifdef 标识符 <br/>程序段1 <br/>#else <br/>程序段2 <br/>#endif <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 它的功能是，如果标识符已被 #define命令定义过则对程序段1进行编译；否则对程序段2进行编译。<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 如果没有程序段2(它为空)，本格式中的#else可以没有， 即可以写为：<br/>#ifdef 标识符 <br/>程序段 <br/>#endif <br/><br/>#define NUM ok<br/>main()&#123;<br/>struct stu<br/>&#123;<br/>int num;<br/>char *name;<br/>char sex;<br/>float score;<br/>&#125; *ps;<br/>ps=(struct stu*)malloc(sizeof(struct stu));<br/>ps->num=102;<br/>ps->name="Zhang ping";<br/>ps->sex='M';<br/>ps->score=62.5;<br/>#ifdef NUM<br/>printf("Number=%d&#92;nScore=%f&#92;n",ps->num,ps->score);<br/>#else<br/>printf("Name=%s&#92;nSex=%c&#92;n",ps->name,ps->sex);<br/>#endif<br/>free(ps);<br/>&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 由于在程序的第16行插入了条件编译预处理命令， 因此要根据NUM是否被定义过来决定编译那一个printf语句。而在程序的第一行已对NUM作过宏定义，因此应对第一个printf语句作编译故运行结果是输出了学号和成绩。<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在程序的第一行宏定义中，定义NUM表示字符串OK，其实也可以为任何字符串，甚至不给出任何字符串，写为：<br/>#define NUM<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 也具有同样的意义。 只有取消程序的第一行才会去编译第二个printf语句。读者可上机试作。<br/>2. 第二种形式：<br/>#ifndef 标识符 <br/>程序段1 <br/>#else <br/>程序段2 <br/>#endif<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 与第一种形式的区别是将“ifdef”改为“ifndef”。它的功能是，如果标识符未被#define命令定义过则对程序段1进行编译， 否则对程序段2进行编译。这与第一种形式的功能正相反。<br/>3. 第三种形式：<br/>#if 常量表达式 <br/>程序段1 <br/>#else <br/>程序段2 <br/>#endif<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 它的功能是，如常量表达式的值为真(非0)，则对程序段1 进行编译，否则对程序段2进行编译。因此可以使程序在不同条件下，完成不同的功能。<br/>#define R 1<br/>main()&#123;<br/>float c,r,s;<br/>printf ("input a number: ");<br/>scanf("%f",&c);<br/>#if R<br/>r=3.14159*c*c;<br/>printf("area of round is: %f&#92;n",r);<br/>#else<br/>s=c*c;<br/>printf("area of square is: %f&#92;n",s);<br/>#endif<br/>&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 本例中采用了第三种形式的条件编译。在程序第一行宏定义中，定义R为1，因此在条件编译时，常量表达式的值为真， 故计算并输出圆面积。<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 上面介绍的条件编译当然也可以用条件语句来实现。 但是用条件语句将会对整个源程序进行编译，生成的目标代码程序很长，而采用条件编译，则根据条件只编译其中的程序段1或程序段2， 生成的目标程序较短。如果条件选择的程序段很长， 采用条件编译的方法是十分必要的。
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] 预处理-条件编译]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>