📄 c93_11.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>#ifdef #ifndef #if #undef </title>
</head>
<body bgcolor="#ccefcc">
<blockquote>
<h4 align="center">#ifdef #else #endif</h4>
<ul>
<li>#ifdef
命令条件依赖于编译器认为这个标识符是否已经预定义过这个事实。</li>
<li>#ifdef 命令控制一组程序行在条件满足的情况下被编译; #else
控制另一组程序行在条件不满足的情况下被编译。<br>
<br>
</li>
</ul>
<p align="center"><img src="../img/c93_11.gif" alt="c93_11.gif (4318 bytes)" WIDTH="290" HEIGHT="199"></p>
<div align="center"><center><table border="6" width="211" cellspacing="0" cellpadding="6" height="156" bordercolor="#FF9933">
<tr>
<th width="435" bgcolor="#FF9933" height="11">例子</th>
</tr>
<tr>
<td ALIGN="center" width="435" bgcolor="#00FFFF" height="117"><p align="left">#ifdef mavis<br>
#include "horse.h"<br>
#define stable 5<br>
#else<br>
#include "ccow.h"<br>
#define stable 15<br>
#endif</td>
</tr>
</table>
</center></div><p>解释<br>
#ifdef 语句说明如果紧接的标识符 (mavis) 已经被预定义,
那么所有一直到 #else的语句都要被编译(如果没有 #else 部分,
则所有一直到 #endif 的语句都要被编译); 如果有 #else, 那么从 #else 到
#endif 的语句在标识符未被定义过的条件下都要被编译。<br>
<br>
你是否发现这样的结构很象 IF_ELSE。</p>
<hr>
<h4 align="center">#ifndef #else #endif</h4>
<blockquote>
<p>#ifndef 和 #if 语句也能与 #else 及 #endif
一起使用。当紧接的标识符未被预定义时, #ifndef 为真。它与 #ifdef
正好相反。</p>
<div align="center"><center><table border="0" width="201">
<tr>
<td width="82" bgcolor="#00FFFF" align="right">#ifdef tt<br>
语句1 <br>
#else <br>
语句2 <br>
#endif </td>
<td width="19">==</td>
<td width="82" bgcolor="#00FFFF">#ifndef tt<br>
语句2 <br>
#else <br>
语句1 <br>
#endif </td>
</tr>
</table>
</center></div><hr>
<h4 align="center">#if #else #endif</h4>
<div align="center"><center><table border="6" width="276" cellspacing="0" cellpadding="6" height="156" bordercolor="#FF9933">
<tr>
<th width="500" bgcolor="#FF9933" height="11">例子</th>
</tr>
<tr>
<td ALIGN="center" width="500" bgcolor="#00FFFF" height="117"><p align="left">#if
machine==1 /* PDP-11 */<br>
printf("We are using PDP-11\n");<br>
#define INT_SIZE 16<br>
#else<br>
#if machine==2 /* VAX-11 */<br>
printf("We are using VAX-11\n");<br>
#define INT_SIZE 32<br>
... ... <br>
#endif /*选择机器*/<br>
#endif</td>
</tr>
</table>
</center></div><hr>
<h4 align="center">#undef</h4>
<p align="left">#undef 与 #define 作用相反。<br>
<br>
例如有如下语句<br>
#define A 1<br>
#define B 2<br>
#define C 3<br>
#undef B</p>
<table border="0" width="404">
<tr>
<td width="150" align="center"></td>
<td width="119" align="center">在 #undef B 前</td>
<td width="123" align="center">在 #undef B 后</td>
</tr>
<tr>
<td width="150" align="center">#ifdef A</td>
<td width="119" align="center">T</td>
<td width="123" align="center">T</td>
</tr>
<tr>
<td width="150" align="center">#ifdef B</td>
<td width="119" align="center">T</td>
<td width="123" align="center">F</td>
</tr>
<tr>
<td width="150" align="center">#ifdef C</td>
<td width="119" align="center">T</td>
<td width="123" align="center">T</td>
</tr>
</table>
<p align="left">怎样使#ifdef语句的结果为TRUE呢? <br>
有两种方法:</p>
<ol>
<li>在 #ifdef 语句之前,
你可以用宏把标识符定义定义为一个任何数字或其它。<br>
---#define IBM_PC 1</li>
<li>或只是简单地定义它。---#define IBM_PC </li>
</ol>
</blockquote>
<p align="center"><a href="javascript:close()">关闭</a></p>
</blockquote>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -