1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
7 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
8 <link rev=
"made" href=
"mailto:scs@eskimo.com">
9 <title>9.3 Conditional Compilation
</title>
10 <link href=
"sx9b.html" rev=precedes
>
11 <link href=
"sx10.html" rel=precedes
>
12 <link href=
"sx9.html" rev=subdocument
>
15 <H2>9.3 Conditional Compilation
</H2>
17 <p>[This section corresponds to K
&R Sec.
4.11.3]
18 </p><p>The last preprocessor directive we're going to look at is
20 If you have the sequence
25 <I>more program text
</I>
29 the code that gets compiled depends on whether a preprocessor macro by that
30 <I>name
</I> is defined or not.
32 (that is, if there has been a
<TT>#define
</TT> line
34 for a macro called
<I>name
</I>),
36 ``
<I>program text
</I>'' is compiled and
37 ``
<I>more program text
</I>'' is ignored.
38 If the macro is not defined,
39 ``
<I>more program text
</I>'' is compiled and
40 ``
<I>program text
</I>'' is ignored.
41 This looks a lot like an
<TT>if
</TT> statement,
42 but it behaves completely differently:
43 an
<TT>if
</TT> statement controls which statements of your
44 program are executed at run time,
45 but
<TT>#ifdef
</TT> controls which parts of your program actually
47 </p><p>Just as for the
<TT>if
</TT> statement, the
<TT>#else
</TT> in an
48 <TT>#ifdef
</TT> is optional.
49 There is a companion directive
<TT>#ifndef
</TT>, which compiles
50 code if the macro is
<em>not
</em> defined
51 (although the ``
<TT>#else
</TT> clause''
52 of an
<TT>#ifndef
</TT> directive will then be compiled if the
53 macro
<em>is
</em> defined).
54 There is also an
<TT>#if
</TT> directive which compiles code
55 depending on whether a compile-time expression is true or false.
56 (The expressions which are allowed in an
<TT>#if
</TT> directive
57 are somewhat restricted,
58 however, so we won't talk much
60 about
<TT>#if
</TT> here.)
61 </p><p>Conditional compilation is useful in two general classes of
63 <UL><li>You are trying to write a portable program,
64 but the way you do something is different depending on what
65 compiler, operating system, or computer you're using.
66 You place different versions of your code,
67 one for each situation,
68 between suitable
<TT>#ifdef
</TT> directives,
69 and when you compile the progam in a particular environment,
70 you arrange to have the macro names defined which select the variants you
71 need in that environment.
72 (For this reason, compilers usually have ways of letting you
73 define macros from the invocation command line or in a
75 and many also predefine certain macro names related to
76 the operating system, processor, or compiler in use.
77 That way, you don't have to change the code to change the
78 <TT>#define
</TT> lines each time you compile it in a different
82 For example, in ANSI C, the function to delete a file is
<TT>remove
</TT>.
83 On older Unix systems, however, the function was called
85 So if
<TT>filename
</TT> is a variable containing the name of a
86 file you want to delete,
87 and if you want to be able to compile the program under these
88 older Unix systems, you might write
96 Then, you could place the line
100 at the top of the file when compiling under an old Unix system.
101 (Since all you're using the macro
<TT>unix
</TT> for is to control
103 you don't need to give it any replacement text at all.
104 <em>Any
</em> definition for a macro,
105 even if the replacement text is empty,
106 causes an
<TT>#ifdef
</TT> to succeed.)
109 (In fact, in this example,
110 you wouldn't even need to define the macro
<TT>unix
</TT> at all,
111 because C compilers on old Unix systems tend to predefine it for you,
112 precisely so you can make tests like these.)
113 <li>You want to compile several different versions of your program,
114 with different features present in the different versions.
115 You bracket the code for each feature with
<TT>#ifdef
</TT>
117 and (as for the previous case)
118 arrange to have the right macros defined or not to build the
119 version you want to build
121 This way, you can build the several different versions from the
123 (One common example is whether you turn debugging statements on
125 You can bracket each debugging printout with
126 <TT>#ifdef DEBUG
</TT> and
<TT>#endif
</TT>,
127 and then turn on debugging only when you need it.)
130 For example, you might use lines like this:
133 printf(
"x is %d\n", x);
136 to print out the value of the variable
<TT>x
</TT> at some point in
137 your program to see if it's what you expect.
138 To enable debugging printouts, you insert the line
142 at the top of the file,
143 and to turn them off, you delete that line,
144 but the debugging printouts quietly remain in your code,
145 temporarily deactivated,
146 but ready to reactivate if you find yourself needing them again later.
147 (Also, instead of inserting and deleting the
<TT>#define
</TT> line,
148 you might use a compiler flag such as
<TT>-DDEBUG
</TT> to define
149 the macro
<TT>DEBUG
</TT> from the compiler invocatin
151 </UL>Conditional compilation can be very handy, but it can also get
153 When large chunks of the program are completely different depending on,
154 say, what operating system the program is being compiled for,
155 it's often better to place the different versions in separate
157 and then only use one of the files
158 (corresponding to one of the versions)
159 to build the program on any given system.
160 Also, if you are using an ANSI Standard compiler and you are
161 writing ANSI-compatible code,
162 you usually won't need so much conditional compilation,
163 because the Standard specifies exactly how the compiler must do certain things,
164 and exactly which library functions it much provide,
165 so you don't have to work so hard to accommodate the old
166 variations among compilers and libraries.
170 <a href=
"sx9b.html" rev=precedes
>prev
</a>
171 <a href=
"sx10.html" rel=precedes
>next
</a>
172 <a href=
"sx9.html" rev=subdocument
>up
</a>
173 <a href=
"top.html">top
</a>
176 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
177 //
<a href=
"copyright.html">Copyright
</a> 1995-
1997
178 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>