source: public/doc/gnu-c/ifdef.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 7.0 KB
Line 
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
4
5(The work of Trevis Rothwell and Nelson Beebe has been assigned or
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing GNU and promoting software freedom." -->
19<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
20<head>
21<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
22<title>ifdef (GNU C Language Manual)</title>
23
24<meta name="description" content="ifdef (GNU C Language Manual)">
25<meta name="keywords" content="ifdef (GNU C Language Manual)">
26<meta name="resource-type" content="document">
27<meta name="distribution" content="global">
28<meta name="Generator" content="makeinfo">
29<link href="index.html" rel="start" title="Top">
30<link href="Symbol-Index.html" rel="index" title="Symbol Index">
31<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
32<link href="Conditional-Syntax.html" rel="up" title="Conditional Syntax">
33<link href="if.html" rel="next" title="if">
34<link href="Conditional-Syntax.html" rel="prev" title="Conditional Syntax">
35<style type="text/css">
36<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.no-bullet {list-style: none}
51-->
52</style>
53
54
55</head>
56
57<body lang="en">
58<span id="ifdef"></span><div class="header">
59<p>
60Next: <a href="if.html" accesskey="n" rel="next">if</a>, Up: <a href="Conditional-Syntax.html" accesskey="u" rel="up">Conditional Syntax</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
61</div>
62<hr>
63<span id="The-_0023ifdef-directive"></span><h4 class="subsubsection">26.6.2.1 The <code>#ifdef</code> directive</h4>
64<span id="index-_0023ifdef"></span>
65<span id="index-_0023endif"></span>
66
67<p>The simplest sort of conditional is
68</p>
69<div class="example">
70<pre class="example">#ifdef <var>MACRO</var>
71
72<var>controlled text</var>
73
74#endif /* <var>MACRO</var> */
75</pre></div>
76
77<span id="index-conditional-group"></span>
78<p>This block is called a <em>conditional group</em>. The body,
79<var>controlled text</var>, will be included in compilation if
80and only if <var>MACRO</var> is defined. We say that the conditional
81<em>succeeds</em> if <var>MACRO</var> is defined, <em>fails</em> if it is not.
82</p>
83<p>The <var>controlled text</var> inside a conditional can include
84preprocessing directives. They are executed only if the conditional
85succeeds. You can nest conditional groups inside other conditional
86groups, but they must be completely nested. In other words,
87<code>#endif</code> always matches the nearest <code>#ifdef</code> (or
88<code>#ifndef</code>, or <code>#if</code>). Also, you cannot start a conditional
89group in one file and end it in another.
90</p>
91<p>Even if a conditional fails, the <var>controlled text</var> inside it is
92still run through initial transformations and tokenization. Therefore,
93it must all be lexically valid C. Normally the only way this matters is
94that all comments and string literals inside a failing conditional group
95must still be properly ended.
96</p>
97<p>The comment following the <code>#endif</code> is not required, but it is a
98good practice if there is a lot of <var>controlled text</var>, because it
99helps people match the <code>#endif</code> to the corresponding <code>#ifdef</code>.
100</p>
101<p>Older programs sometimes put <var>macro</var> directly after the
102<code>#endif</code> without enclosing it in a comment. This is invalid code
103according to the C standard, but it only causes a warning in GNU C.
104It never affects which <code>#ifndef</code> the <code>#endif</code> matches.
105</p>
106<span id="index-_0023ifndef"></span>
107<p>Sometimes you wish to use some code if a macro is <em>not</em> defined.
108You can do this by writing <code>#ifndef</code> instead of <code>#ifdef</code>.
109One common use of <code>#ifndef</code> is to include code only the first
110time a header file is included. See <a href="Once_002dOnly-Headers.html">Once-Only Headers</a>.
111</p>
112<p>Macro definitions can vary between compilations for several reasons.
113Here are some samples.
114</p>
115<ul>
116<li> Some macros are predefined on each kind of machine
117(see <a href="https://gcc.gnu.org/onlinedocs/gcc/System_002dspecific-Predefined-Macros.html#System_002dspecific-Predefined-Macros">System-specific Predefined Macros</a> in <cite>Using the GNU Compiler
118Collection</cite>). This allows you to provide code specially tuned for a
119particular machine.
120
121</li><li> System header files define more macros, associated with the features
122they implement. You can test these macros with conditionals to avoid
123using a system feature on a machine where it is not implemented.
124
125</li><li> Macros can be defined or undefined with the <samp>-D</samp> and <samp>-U</samp>
126command-line options when you compile the program. You can arrange to
127compile the same source file into two different programs by choosing a
128macro name to specify which program you want, writing conditionals to
129test whether or how this macro is defined, and then controlling the
130state of the macro with command-line options, perhaps set in the
131file <samp>Makefile</samp>. See <a href="https://gcc.gnu.org/onlinedocs/gcc/Invocation.html#Invocation">Invoking GCC</a> in <cite>Using the GNU Compiler Collection</cite>.
132
133</li><li> Your program might have a special header file (often called
134<samp>config.h</samp>) that is adjusted when the program is compiled. It can
135define or not define macros depending on the features of the system and
136the desired capabilities of the program. The adjustment can be
137automated by a tool such as <code>autoconf</code>, or done by hand.
138</li></ul>
139
140<hr>
141<div class="header">
142<p>
143Next: <a href="if.html" accesskey="n" rel="next">if</a>, Up: <a href="Conditional-Syntax.html" accesskey="u" rel="up">Conditional Syntax</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
144</div>
145
146
147
148</body>
149</html>
Note: See TracBrowser for help on using the repository browser.