⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 编译预处理.htm

📁 语言基础(初学都必修)包括数据类型、运算符与表达式,运算符和表达式,逻辑运算符和逻辑表达式,C语言中的关键字,定义宏和使用宏定义,数据类型转换
💻 HTM
📖 第 1 页 / 共 3 页
字号:
    <tr>
      <td width=757 height=20>
      <P align=center>作者:佚名&nbsp;&nbsp;&nbsp;&nbsp;教程来源:网络&nbsp;&nbsp;&nbsp;&nbsp;点击数:408&nbsp;&nbsp;&nbsp;&nbsp;更新时间:2006-6-15 <A title=收藏的网页将被永久地保存到新浪ViVi收藏夹http://vivi.sina.com.cn href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(vivi=window.open('http://vivi.sina.com.cn/collect/icollect.php?pid=2008&title='+escape(d.title)+'&url='+escape(d.location.href)+'&desc='+escape(t),'vivi','scrollbars=no,width=480,height=480,left=75,top=20,status=no,resizable=yes'));vivi.focus();"><IMG src="/images/vivi_coop1.gif" align=absMiddle border=0></A> </P></td>
    </tr>
    <tr>
      <td style="BORDER-TOP-STYLE: solid; BORDER-RIGHT-STYLE: solid; BORDER-LEFT-STYLE: solid; BORDER-BOTTOM-STYLE: solid" borderColor=#7c8bc7 width=758 >
      <div align="center">
		<table cellpadding="0" cellspacing="0" width="96%" height="100%">
			<!-- MSTableType="layout" -->
			<tr>
				<td height="17" valign="top">
				<p align="left"><P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN style="FONT-FAMILY: 楷体_GB2312">在前面各章中,程序的开头经常使用以“<SPAN lang=EN-US>#”号开头的命令。例如# include,这些命令是在源程序正式编译前进行处理的,称为“预处理命令”。预处理功能主要包括宏定义、文件包含和条件编译三种。通过本章学习,要求读者了解预处理命令的特点;掌握宏定义的方法;学会包含文件的处理和条件编译的使用。<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">为了优化程序设计环境,提高编程效率,</SPAN><SPAN lang=EN-US>ANSI C</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">标准允许在</SPAN><SPAN lang=EN-US>C</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">源程序中加入一些“预处理命令”。一般来说,大部分高级语言源程序要通过编译程序将源程序“翻译”成目标代码程序,再通过连接生成可执行文件。编译程序的作用就是进行语法分析、词法分析、产生目标代码并进行代码优化等处理。在编译时,首先对源程序进行第一遍扫描,预先处理源程序中以“</SPAN><SPAN lang=EN-US>#</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">”号开头的命令。经过预处理后程序不再包括预处理命令,最后再由编译程序对预处理后的源程序进行通常的编译处理,得到可执行的目标代码。</SPAN></P>
<H2 style="MARGIN: 8pt 0cm; LINE-HEIGHT: normal; TEXT-ALIGN: center" align=center><SPAN lang=EN-US style="FONT-SIZE: 15pt; mso-bidi-font-size: 16.0pt">*1</SPAN><SPAN style="FONT-SIZE: 15pt; FONT-FAMILY: 黑体; mso-ascii-font-family: Arial; mso-bidi-font-size: 16.0pt">宏定义</SPAN><SPAN lang=EN-US style="FONT-SIZE: 15pt; mso-bidi-font-size: 16.0pt"><o:p></o:p></SPAN></H2>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在</SPAN><SPAN lang=EN-US>C</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">语言源程序中用一个指定的标识符(即名字)来代表一个字符串称为“宏”,这个标识符称为“宏名”。在源程序中可以出现这个宏名,称为“宏引用”或“宏调用”。在编译预处理时,对源程序中所有出现的“宏名”,均可用宏定义中的字符串去替换,这种将宏名替换成字符串的过程称为“宏替换”或“宏展开”。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">为了区别于一般的变量名、数组名、指针变量名,宏名通常用大写字母组成。宏分为无参数的宏(即无参宏)和有参数的宏(即有参宏)两种。</SPAN></P>
<H3 style="MARGIN: 4pt 0cm; LINE-HEIGHT: normal"><SPAN lang=EN-US style="FONT-SIZE: 14pt; FONT-FAMILY: 方正姚体; mso-bidi-font-size: 16.0pt">**1.1不带参数的宏<o:p></o:p></SPAN></H3>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">不带参数的宏定义的一般形式为:</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 52.7pt; mso-char-indent-count: 5.0; mso-char-indent-size: 10.5pt"><B><SPAN lang=EN-US style="FONT-FAMILY: 宋体">#define<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>宏名<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>字符串<o:p></o:p></SPAN></B></P>
<P class=MsoNormal><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这就是我们前面已经介绍过的定义符号常量,例如:</SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>#define<SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>PRICE<SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>150<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN style="FONT-FAMILY: 宋体">其功能是指定标识符<SPAN lang=EN-US>PRICE来代替“150”这个字符串,源程序开始编译前,将会把源程序中所有引用PRICE宏名替换成“150”字符串,然后再将编译源程序。<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN style="FONT-FAMILY: 宋体">【<B>例<SPAN lang=EN-US>1</SPAN></B>】不带参数的宏应用。<SPAN lang=EN-US><o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>#define<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>PRICE<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>150<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>main(<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>{ int<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>num,total;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>printf("input num:\n");<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>scanf("%d",&amp;num);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;</SPAN>total=PRICE*num;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>printf("Total money is:%d yuan\n",total);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 11.5pt; FONT-FAMILY: 宋体; mso-bidi-font-size: 12.0pt">}<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN style="FONT-SIZE: 11.5pt; FONT-FAMILY: 宋体; mso-bidi-font-size: 12.0pt">运行情况如下:<SPAN lang=EN-US><o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US style="FONT-SIZE: 11.5pt; FONT-FAMILY: 宋体; mso-bidi-font-size: 12.0pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>input num:<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US style="FONT-SIZE: 11.5pt; FONT-FAMILY: 宋体; mso-bidi-font-size: 12.0pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>20</SPAN><SPAN style="FONT-FAMILY: 宋体">↙<SPAN lang=EN-US><o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Total money is:3000 yuan<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 24pt; mso-char-indent-count: 2.0; mso-char-indent-size: 12.0pt"><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 黑体; mso-hansi-font-family: 宋体">说明:<SPAN lang=EN-US><o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN style="FONT-FAMILY: 宋体">(<SPAN lang=EN-US>1)宏名一般用大写字母组成,宏名的前后应有空格,以便准确地辨认宏名。<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">(<SPAN lang=EN-US>2)宏定义不是语句,其后不要跟分号(;),如果加了分号则会连分号一起进行替换。例如:<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>#define PRICE<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>150;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>total=PRICE*num;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">经过宏展开后,该语句为:<SPAN lang=EN-US><o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 57.75pt; mso-char-indent-count: 5.5; mso-char-indent-size: 10.5pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">total=150;*num;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">这显然将会出现语法错误。<SPAN lang=EN-US><o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">(<SPAN lang=EN-US>3)宏定义必须写在函数之外,通常#define命令写在文件开头、函数之前。宏名作用域为从宏定义之处开始,到本源程序结束。若要终止其作用域,可以使用#endif命令。如下所示:<o:p></o:p></SPAN></SPAN></P>
<DIV align=center>
<TABLE style="BORDER-COLLAPSE: collapse; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt" cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR style="HEIGHT: 131.55pt">
<TD style="PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; WIDTH: 275.4pt; PADDING-TOP: 0cm; HEIGHT: 131.55pt" vAlign=top width=367>
<P class=MsoNormal style="TEXT-INDENT: 20pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.0pt"><?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><v:group id=_x0000_s1036 style="MARGIN-TOP: 4pt; Z-INDEX: 3; LEFT: 0px; MARGIN-LEFT: 167.9pt; WIDTH: 0.25pt; POSITION: absolute; HEIGHT: 81.5pt; TEXT-ALIGN: left" coordsize="5,1630" coordorigin="6665,7136"><v:line id=_x0000_s1028 style="POSITION: absolute" from="6665,7993" to="6665,8766"><v:stroke endarrow="block" endarrowwidth="narrow" endarrowlength="short"></v:stroke></v:line><v:line id=_x0000_s1029 style="POSITION: absolute; flip: y" from="6670,7136" to="6670,7604"><v:stroke endarrow="block" endarrowwidth="narrow" endarrowlength="short"></v:stroke></v:line></v:group><v:line id=_x0000_s1026 style="Z-INDEX: 1; LEFT: 0px; POSITION: absolute; TEXT-ALIGN: left" from="149.625pt,7.25pt" to="185.625pt,7.25pt"><v:stroke endcap="round" dashstyle="shortDot"></v:stroke></v:line><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">#define<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>PRICE<SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>150<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 14.9pt; mso-char-indent-count: 1.42; mso-char-indent-size: 10.45pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>main(<SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>{<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>PRICE的有效范围<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 36.75pt; tab-stops: 167.65pt"><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">……<SPAN lang=EN-US><o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 19.8pt; mso-char-indent-count: 1.98; mso-char-indent-size: 10.0pt"><v:line id=_x0000_s1027 style="Z-INDEX: 2; LEFT: 0px; POSITION: absolute; TEXT-ALIGN: left" from="149.625pt,7.25pt" to="185.625pt,7.25pt"><v:stroke endcap="round" dashstyle="shortDot"></v:stroke></v:line><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">#undef PRICE<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">fun(<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 26.25pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>{<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>……<SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P></TD></TR></TBODY></TABLE></DIV>
<P class=MsoNormal><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">在这里,<SPAN lang=EN-US>PRICE只在main函数中有效,而在fun函数中无效。<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">(<SPAN lang=EN-US>4)宏定义是用宏名来表示一个字符串,在宏展开时又以该字符串取代宏名,这只是一种简单的替换,不做任何检查。宏定义时“字符串”可以是常数、表达式,也可以含任何字符,要注意替换后的结果,通常可以在合适的位置上加括号。例如:<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>#define<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>PRICE<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>100+50<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0cm; TEXT-INDENT: 21pt; MARGIN-RIGHT: 0cm; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>……<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>total=PRICE*num;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">该语句“<SPAN lang=EN-US>total=PRICE*num;”宏替换后形成的语句是“total=100+50*num;”,运算结果会出错。如果定义时改为“#define<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>PRICE<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>(100+50)”,则宏替换后形成的语句是“total=(100+50)*num;”,运算结果才是正确的。<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">(<SPAN lang=EN-US>5)在宏定义的字符串中可以出现已经定义过的另一个宏名,称为嵌套宏定义。例如:<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>#define<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>NUM<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>20<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>#define<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>PRICE<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>150<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>#define<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>TOTAL<SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>PRICE*NUM<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 0cm; TEXT-INDENT: 21pt; MARGIN-RIGHT: 0cm; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>  ……<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; LINE-HEIGHT: 150%; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>printf(</SPAN><SPAN style="FONT-FAMILY: 宋体">"<SPAN lang=EN-US>total=%d\n",TOTAL);</SPAN></SPAN><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">最后一个语句经过宏展开后为“<SPAN lang=EN-US>printf(</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体">"<SPAN lang=EN-US>total=%d\n",150*20);”。</SPAN></SPAN><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">(<SPAN lang=EN-US>6)C语言规定,宏名如果出现在字符串常量中,将不作为宏名处理,不对其进行宏替换。例如:<o:p></o:p></SPAN></SPAN></P>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -