C preprocessor: Difference between revisions

[unchecked revision][unchecked revision]
Content deleted Content added
No edit summary
m Bot: Replace deprecated source tag with syntaxhighlight
Line 25:
The preprocessor can ''define'' tokens. It is good custom to write these tokens in ALL CAPS. (See pt. 2 as for why.)
 
<sourcesyntaxhighlight lang="c">
#define MYTOKEN
</syntaxhighlight>
</source>
 
Most compilers also allow the definition of preprocessor tokens on the command line, e.g. the "-D MYTOKEN" option for [[GCC]].
Line 34:
The preprocessor can ''conditionally'' select which parts of source code to compile, depending on whether a given token is defined or not (see above).
 
<sourcesyntaxhighlight lang="c">
#define MYTOKEN
 
Line 46:
/* This source will be compiled */
#endif
</syntaxhighlight>
</source>
 
Note that such ''#if'' / ''#ifdef'' / ''#ifndef'' - ''#endif'' sections can be nested.
Line 55:
The solution are ''header guards'', a combination of conditional compilation and token definition:
 
<sourcesyntaxhighlight lang="c">
/* abc.h */
 
Line 64:
 
#endif
</syntaxhighlight>
</source>
 
== Preprocessor Macros, pt. 2 ==
Line 73:
The ''#if'' statement can be used to base conditional compilation on token values. Note that the preprocessor can only work with compile-time constants. Compiler-evaluated code like `sizeof()` cannot be used in preprocessor directives. On the upside, the preprocessor can natively handle non-numerical values.
 
<sourcesyntaxhighlight lang="c">
#define MYTOKEN foo
#define OTHERTOKEN 42
Line 90:
/* Won't be compiled. */
#endif
</syntaxhighlight>
</source>
 
The ''#if'' directive also allows for a simple construct to disable a region of code without having to worry about nested ''/* ... */'' style comments:
 
<sourcesyntaxhighlight lang="c">
#if 0
/* disabled code */
#endif
</syntaxhighlight>
</source>
 
Such code can easily be re-enabled temporarily with no more effort than replacing the "0" with a "1". Source comments as to why you disabled code this way are in order.
Line 144:
For production code, assertions may be turned off by defining NDEBUG:
 
<sourcesyntaxhighlight lang="bash">
gcc -DNDEBUG ...
</syntaxhighlight>
</source>
 
Note that <assert.h> does not have (or need) a header guard, i.e. can be included multiple times in a source file, and that whether NDEBUG is defined or not is evaluated anew ''at every inclusion of <assert.h>''. You can thus enable / disable assertions at a very fine-grained level if necessary: