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

📄 c91.htm

📁 经典c语言教程
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>宏定义 </title>
<script language="javascript">
   var prePage="c/c8/c84.htm";
   var nextPage="c/c9/c92.htm";
	function showwin(url,winname,properties){
		window.open(url,winname,properties)
	}

	function init(){
		moreexam=moreexamdiv.style;
	}
	function show(obj){
		obj.display='';
	}
	function hide(obj){
		obj.display='none';
	}
	

</script>

<link rel="stylesheet" href="../cstyle.css" type="text/css">
<bgsound src="../voice/c91.au" loop="1">
</head>

<body background="../img/mainback.jpg" bgproperties="fixed" onLoad="init()">

<h2 align="center"><a name="_top"></a><font face="楷体_GB2312">9.1 宏定义</font></h2>

<table border="0" width="100%" cellspacing="0" cellpadding="0">
  <tr>
    <td width="50%" align="center"><a href="c91.htm#c911.html#c911">宏定义</a></td>
    <td width="50%" align="center"><a href="c91.htm#c912.html#c912">练习题</a></td>
  </tr>
</table>

<hr>

<h3><a name="c911"></a>1.宏定义</h3>

<blockquote>
  <p>在这一课里, 你将学习两种宏定义类型。它们是: <ul>
    <li>符号常量</li>
    <li>带参数的宏</li>
  </ul>
  <p>现在, 让我们看看符号常量。<br>
  <font color="#008080">#define 符号名 字符串常量</font><br>
  <br>
  #define 预处理语句象其它的预处理语句一样,以 # 
  符号作为开头。它可以出现在源文件的任何地方。用 #define 
  定义的名字的 &quot;作用域&quot; 是从它定义点开始到源文件结尾。<br>
  <br>
  例子 #define ONE 1&nbsp; 在源文件经过预处理后, ONE 将被 1代替。但是引号内并不进行这样的替代。<br>
  <br>
  例子<br>
  #define TWO 2<br>
  #define MSG &quot;I love c&quot;<br>
  #define FMT &quot;x is %d\n&quot;<br>
  main()<br>
  {<br>
  &nbsp;&nbsp;&nbsp; int x=TWO;<br>
  &nbsp;&nbsp;&nbsp; printf(FMT,x);<br>
  &nbsp;&nbsp;&nbsp; printf(&quot;%s\n&quot;,MSG);<br>
  }<br>
  这个例子的输出是:<br>
  x is 2<br>
  I love c</p>
  <p>#define中的名字同 C 语言中的标识符形式一样, 
  置换的文本是任意的。一个长的定义应该在行结尾处加一个续行符 \, 
  以示续行。<br>
  在这个例子中: TWO 被 2 置换, MSG 被 &quot;I love c&quot; 置换, FMT 被 &quot;x 
  is %d\n&quot; 置换。如果你不用 #define, 那么:<br>
  main()<br>
  {<br>
  &nbsp;&nbsp;&nbsp; int x=2;<br>
  &nbsp;&nbsp;&nbsp; printf(&quot;x is %d\n&quot;,x);<br>
  &nbsp;&nbsp;&nbsp; printf(&quot;%s\n&quot;,&quot;I love c&quot;);<br>
  }<br>
  这个程序和上边的那个相同。<br>
  <br>
  <br>
  符号常量很简单是吧? 让我们更进一步, 来学习带参数的宏定义。<br>
  <br>
  #define 也可以用来定义带参量的宏, 正象下面这样<br>
  #define max(a,b) a&gt;b?a:b<br>
  这个宏被用来得到 a 和 b 中较大的数。只要max(x,y) 
  出现在程序中的某处, 预处理程序就把它置换为: x&gt;y?x:y <br>
  <br>
  宏的定义是很简单的, 但是如果你使用时如果不小心, 
  也许你会出错误。请看:<br>
  在 main() 之前你定义了:&nbsp; #define A(x) x*x <br>
  在程序中, 你想计算 (y+1)*(y+1), 那么可能你会用 A(y+1) 
  来求得结果。但这是错的,因为 A(y+1)=y+1*y+1。你可以使 A(y+1) 等于 
  (y+1)*(y+1) 的, 只要简单地加上括号:<br>
  &nbsp;&nbsp;&nbsp; <font color="#000080">#define A(x) (x)*(x)</font></p>
  <p><a href="javascript:show(moreexam)">更多的例子:</a><img
  src="../img/lefthand.gif" alt="lefthand.jpg (983 bytes)" WIDTH="45" HEIGHT="20"></p>
  <div id="moreexamdiv" style="display='none'"><p><font color="#000080">#define pai 3.14159<br>
  #define s(r) pai*(r)*(r)<br>
  #define l(r) 2*pai*(r)<br>
  #define sqr(x) (x)*(x)<br>
  #define max(x,y) x&gt;y?x:y<br>
  #define p(x) printf(&quot;%d\n\<br>
  &nbsp;&nbsp;&nbsp; &quot;,x)</font></p>
  </div><h4 align="center">使用规则</h4>
  <ul>
    <li><p align="left">不需要象其它语句一样在#define 命令的后面加分号, 
      但是如果宏定义需要, 表达式也可以包括分号.</p>
    </li>
    <li><p align="left">宏定义的符号变量可被使用在标识符和表达式里。当程序中使用宏定义时,宏的参数将被实际的内容替代。<br>
      </p>
    </li>
  </ul>
  <p align="right"><a href="c91.htm#_top.html#_top">返回页首</a></p>
</blockquote>

<hr>

<h3><a name="c912"></a>2.练习题</h3>

<blockquote>
  <p><a
  href="javascript:showwin('c91_21.htm',null,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,copyhistory=0,width=700,height=500')">练习题</a><img
  src="../img/lefthand.gif" alt="lefthand.jpg (983 bytes)" WIDTH="45" HEIGHT="20"></p>
</blockquote>

<blockquote>
  <p align="right"><a href="c91.htm#_top.html#_top">返回页首</a></p>
</blockquote>

<p align="center"><a href="http://www.nec.sjtu.edu.cn/support/Course/C/c/c9/c92.htm"><img src="../img/next.gif" width="145" height="30"
alt="next.gif (3633 bytes)" border="0"></a></p>
</body>
</html>

⌨️ 快捷键说明

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