Files
oldlinux-files/Ref-docs/c_lib_guide/1.7.html
2024-02-19 00:21:47 -05:00

278 lines
8.6 KiB
HTML

<html>
<head>
<title>
C Guide--1.7 Preprocessing Directives
</title>
<!-- Changed by: eric huss, 12-Mar-1997 -->
</head>
<body text="#000000" bgcolor="#FFFFFF">
<center>
<table border=0 width=100%>
<tr>
<td align=left width=20% valign=top>
<a href="1.6.html">
<img src="left.gif" border=0>
Previous Section<br>
1.6 Statements</a></td>
<td align=center width=60% valign=top>
| <a href="index.html">Table of Contents</a> |
<a href="index2.html">Index</a> |</td>
<td align=right width=20% valign=top>
<a href="2.1.html">
Next Section
<img src="right.gif" border=0><br>
2.1 assert.h</a></td>
</tr>
</table>
</center>
<hr>
<a name="conditional"></a>
<h1>1.7.1 #if, #elif, #else, #endif</h1>
<p>
These preprocessing directives create conditional compiling parameters that control the
compiling of the source code. They must begin on a separate line.
<p>
Syntax:<br>
<blockquote>
<code><b>
#if</code></b> <i>constant_expression</i><br>
<code><b>#else<br>
#endif<br>
<br>
or<br>
<br>
#if</code></b> <i>constant_expression</i><br>
<code><b>#elif</code></b> <i>constant_expression</i><br>
<code><b>#endif</code></b><br>
</blockquote>
The compiler only compiles the code after the <b><code>#if</code></b> expression if the <i>constant_expression</i>
evaluates to a non-zero value (true). If the value is 0 (false), then the compiler skips the lines
until the next <b><code>#else</code></b>, <b><code>#elif</code></b>, or <b><code>#endif</code></b>. If there is a matching <b><code>#else</code></b>, and the <i>constant_expression</i>
evaluated to 0 (false), then the lines between the <b><code>#else</code></b> and the <b><code>#endif</code></b> are compiled. If there is a
matching <b><code>#elif</code></b>, and the preceding <b><code>#if</code></b> evaluated to false, then the <i>constant_expression</i> after that
is evaluated and the code between the <b><code>#elif</code></b> and the <b><code>#endif</code></b> is compiled only if this expression
evaluates to a non-zero value (true).
<p>
Examples:
<blockquote><code><b><pre>
int main(void)
{
#if 1
printf("Yabba Dabba Do!\n");
#else
printf("Zip-Bang!\n");
#endif
return 0;
}
</pre></b></code></blockquote>
Only "Yabba Dabba Do!" is printed.
<blockquote><code><b><pre>
int main(void)
{
#if 1
printf("Checkpoint1\n");
#elif 1
printf("Checkpoint2\n");
#endif
return 0;
}
</pre></b></code></blockquote>
Only "Checkpoint1" is printed. Note that if the first line is #if 0, then only "Checkpoint2" would
be printed.
<blockquote><code><b><pre>
#if OS==1
printf("Version 1.0");
#elif OS==2
printf("Version 2.0");
#else
printf("Version unknown");
#endif
</pre></b></code></blockquote>
Prints according to the setting of OS which is defined with a #define.
<br>
<a name="define"></a>
<h1> 1.7.2 #define, #undef, #ifdef, #ifndef</h1>
<p>
The preprocessing directives <b><code>#define</b></code> and <b><code>#undef</b></code> allow the definition of identifiers which
hold a certain value. These identifiers can simply be constants or a macro function. The
directives <b><code>#ifdef</b></code> and <b><code>#ifndef</b></code> allow conditional compiling of certain lines of code based on
whether or not an identifier has been defined.
<p>
Syntax:<br>
<blockquote><code><b>
#define</b></code> <i>identifier replacement-code</i><br>
<br>
<code><b>#undef</b></code> <i>identifier</i><br>
<br>
<code><b> #ifdef</b></code> <i>identifier</i><br>
<code><b>#else</b></code> or <code><b>#elif</b></code><br>
<code><b>#endif</b></code><br>
<br>
<code><b>#ifndef</b></code> <i>identifier</i><br>
<code><b>#else</b></code> or <code><b>#elif</b></code><br>
<code><b>#endif</b></code><br>
<br>
<code><b>#ifdef</b></code> <i>identifier</i> is the same is <code><b>#if defined(</b></code> <i>identifier</i><code><b>)</b></code>.<br>
<code><b>#ifndef</b></code> <i>identifier</i> is the same as <code><b>#if
!defined(</b></code><i>identifier</i><code><b>)</b></code>.<br>
An identifier defined with <code><b>#define</b></code> is available anywhere in the source code until a
<code><b>#undef</b></code> is reached.<br>
A function macro can be defined with <code><b>#define</b></code> in the following manner:<br>
<br>
<code><b>#define</b></code> <i>identifier</i><code><b>(</b></code><i>parameter-list</i><code><b>) (</b></code><i>replacement-text</i><code><b>)</b></code><br>
<br>
The values in the <i>parameter-list</i> are replaced in the <i>replacement-text</i>.<br>
<br>
</blockquote>
Examples:
<blockquote><code><b><pre>
#define PI 3.141
printf("%f",PI);
#define DEBUG
#ifdef DEBUG
printf("This is a debug message.");
#endif
#define QUICK(x) printf("%s\n",x);
QUICK("Hi!")
#define ADD(x, y) x + y
z=3 * ADD(5,6)
</pre></b></code></blockquote>
This evaluates to 21 due to the fact that multiplication takes precedence over addition.
<blockquote><code><b><pre>
#define ADD(x,y) (x + y)
z=3 * ADD(5,6)
</pre></b></code></blockquote>
This evaluates to 33 due to the fact that the summation is encapsulated in parenthesis which
takes precedence over multiplication.
<p>
<a name="include"></a>
<h1> 1.7.3 #include</h1>
<p>
The <b><code>#include</code></b> directive allows external header files to be processed by the compiler.
<p>
Syntax:<br>
<blockquote><code><b>
#include &lt;</b></code><i>header-file</i><code><b>&gt;<br>
<br>
or<br>
<br>
#include "</b></code><i>source-file</i><code><b>"</b></code><br>
</blockquote>
When enclosing the file with &lt; and &gt;, then the implementation searches the known
header directories for the file (which is implementation-defined) and processes it. When
enclosed with double quotation marks, then the entire contents of the source-file is replaced at
this point. The searching manner for the file is implementation-specific.
<p>
Examples:
<blockquote><code><b>
#include &lt;stdio.h&gt;<br>
#include "my_header.h"<br>
</b></code></blockquote>
<a name="line"></a>
<h1> 1.7.4 #line</h1>
<p>
The <code><b>#line</b></code> directive allows the current line number and the apparent name of the current
sourcecode filename to be changed.
<p>
Syntax:
<blockquote><code><b>
#line</b></code> <i>line-number filename</i>
</blockquote>
Note that if the filename is not given, then it stays the same. The line number on the
current line is one greater than the number of new-line characters (so the first line number is 1).
<br>
Examples:
<blockquote><code><b>
#line 50 user.c<br>
<br>
#line 23<br>
</b></code></blockquote>
<a name="error"></a>
<h1> 1.7.5 #error</h1>
<p>
The <code><b>#error</b></code> directive will cause the compiler to halt compiling and return with the
specified error message.
<p>
Syntax:
<blockquote><code><b>
#error</b></code> <i> message</i>
</blockquote>
Examples:
<blockquote><code><b>
#ifndef VERSION<br>
#error Version number not specified.<br>
#endif<br>
</b></code></blockquote>
<a name="pragma"></a>
<h1> 1.7.6 #pragma</h1>
<p>
This <code><b>#pragma</b></code> directive allows a directive to be defined. Its effects are
implementation-defined. If the pragma is not supported, then it is ignored.
<p>
Syntax:
<blockquote><code><b>
#pragma</b></code> <i>directive</i>
</blockquote>
<a name="macros"></a>
<h1> 1.7.7 Predefined Macros</h1>
<p>
The following macros are already defined by the compiler and cannot be changed.
<table border=0>
<tr>
<td valign=top>__LINE__</td>
<td>A decimal constant representing the current line number.</td>
</tr>
<tr>
<td valign=top>__FILE__</td>
<td>A string representing the current name of the source code file.</td>
</tr>
<tr>
<td valign=top>__DATE__</td>
<td>A string representing the current date when compiling began for the current
source file. It is in the format "mmm dd yyyy", the same as what is generated by
the asctime function.</td>
</tr>
<tr>
<td valign=top>__TIME__</td>
<td>A string literal representing the current time when cimpiling began for the current
source file. It is in the format "hh:mm:ss", the same as what is generated by the
asctime function.</td>
</tr>
<tr>
<td valign=top>__STDC__</td>
<td>The decimal constant 1. Used to indicate if this is a standard C compiler.</td>
</tr>
</table>
<hr>
<center>
<table border=0 width=100%>
<tr>
<td align=left width=20% valign=top>
<a href="1.6.html">
<img src="left.gif" border=0>
Previous Section<br>
1.6 Statements</a></td>
<td align=center width=60% valign=top>
| <a href="index.html">Table of Contents</a> |
<a href="index2.html">Index</a> |</td>
<td align=right width=20% valign=top>
<a href="2.1.html">
Next Section
<img src="right.gif" border=0><br>
2.1 assert.h</a></td>
</tr>
</table>
</center>
</body>
</html>