blob: 0e1fc76aa9cf6a39104cd5141abb7394eff0bf74 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 1987-2015 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation. A copy of
the license is included in the
section entitled "GNU Free Documentation License".
This manual contains no Invariant Sections. The Front-Cover Texts are
(a) (see below), and the Back-Cover Texts are (b) (see below).
(a) The FSF's Front-Cover Text is:
A GNU Manual
(b) The FSF's Back-Cover Text is:
You have freedom to copy and modify this GNU Manual, like GNU
software. Copies published by the Free Software Foundation raise
funds for GNU development. -->
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>The C Preprocessor: Ifdef</title>
<meta name="description" content="The C Preprocessor: Ifdef">
<meta name="keywords" content="The C Preprocessor: Ifdef">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Index-of-Directives.html#Index-of-Directives" rel="index" title="Index of Directives">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Conditional-Syntax.html#Conditional-Syntax" rel="up" title="Conditional Syntax">
<link href="If.html#If" rel="next" title="If">
<link href="Conditional-Syntax.html#Conditional-Syntax" rel="prev" title="Conditional Syntax">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Ifdef"></a>
<div class="header">
<p>
Next: <a href="If.html#If" accesskey="n" rel="next">If</a>, Up: <a href="Conditional-Syntax.html#Conditional-Syntax" accesskey="u" rel="up">Conditional Syntax</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index-of-Directives.html#Index-of-Directives" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Ifdef-1"></a>
<h4 class="subsection">4.2.1 Ifdef</h4>
<a name="index-_0023ifdef"></a>
<a name="index-_0023endif"></a>
<p>The simplest sort of conditional is
</p>
<div class="smallexample">
<pre class="smallexample">#ifdef <var>MACRO</var>
<var>controlled text</var>
#endif /* <var>MACRO</var> */
</pre></div>
<a name="index-conditional-group"></a>
<p>This block is called a <em>conditional group</em>. <var>controlled text</var>
will be included in the output of the preprocessor if and only if
<var>MACRO</var> is defined. We say that the conditional <em>succeeds</em> if
<var>MACRO</var> is defined, <em>fails</em> if it is not.
</p>
<p>The <var>controlled text</var> inside of a conditional can include
preprocessing directives. They are executed only if the conditional
succeeds. You can nest conditional groups inside other conditional
groups, but they must be completely nested. In other words,
&lsquo;<samp>#endif</samp>&rsquo; always matches the nearest &lsquo;<samp>#ifdef</samp>&rsquo; (or
&lsquo;<samp>#ifndef</samp>&rsquo;, or &lsquo;<samp>#if</samp>&rsquo;). Also, you cannot start a conditional
group in one file and end it in another.
</p>
<p>Even if a conditional fails, the <var>controlled text</var> inside it is
still run through initial transformations and tokenization. Therefore,
it must all be lexically valid C. Normally the only way this matters is
that all comments and string literals inside a failing conditional group
must still be properly ended.
</p>
<p>The comment following the &lsquo;<samp>#endif</samp>&rsquo; is not required, but it is a
good practice if there is a lot of <var>controlled text</var>, because it
helps people match the &lsquo;<samp>#endif</samp>&rsquo; to the corresponding &lsquo;<samp>#ifdef</samp>&rsquo;.
Older programs sometimes put <var>MACRO</var> directly after the
&lsquo;<samp>#endif</samp>&rsquo; without enclosing it in a comment. This is invalid code
according to the C standard. CPP accepts it with a warning. It
never affects which &lsquo;<samp>#ifndef</samp>&rsquo; the &lsquo;<samp>#endif</samp>&rsquo; matches.
</p>
<a name="index-_0023ifndef"></a>
<p>Sometimes you wish to use some code if a macro is <em>not</em> defined.
You can do this by writing &lsquo;<samp>#ifndef</samp>&rsquo; instead of &lsquo;<samp>#ifdef</samp>&rsquo;.
One common use of &lsquo;<samp>#ifndef</samp>&rsquo; is to include code only the first
time a header file is included. See <a href="Once_002dOnly-Headers.html#Once_002dOnly-Headers">Once-Only Headers</a>.
</p>
<p>Macro definitions can vary between compilations for several reasons.
Here are some samples.
</p>
<ul>
<li> Some macros are predefined on each kind of machine
(see <a href="System_002dspecific-Predefined-Macros.html#System_002dspecific-Predefined-Macros">System-specific Predefined Macros</a>). This allows you to provide
code specially tuned for a particular machine.
</li><li> System header files define more macros, associated with the features
they implement. You can test these macros with conditionals to avoid
using a system feature on a machine where it is not implemented.
</li><li> Macros can be defined or undefined with the <samp>-D</samp> and <samp>-U</samp>
command line options when you compile the program. You can arrange to
compile the same source file into two different programs by choosing a
macro name to specify which program you want, writing conditionals to
test whether or how this macro is defined, and then controlling the
state of the macro with command line options, perhaps set in the
Makefile. See <a href="Invocation.html#Invocation">Invocation</a>.
</li><li> Your program might have a special header file (often called
<samp>config.h</samp>) that is adjusted when the program is compiled. It can
define or not define macros depending on the features of the system and
the desired capabilities of the program. The adjustment can be
automated by a tool such as <code>autoconf</code>, or done by hand.
</li></ul>
<hr>
<div class="header">
<p>
Next: <a href="If.html#If" accesskey="n" rel="next">If</a>, Up: <a href="Conditional-Syntax.html#Conditional-Syntax" accesskey="u" rel="up">Conditional Syntax</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index-of-Directives.html#Index-of-Directives" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>