Conditional Compilation

Conditional compilation directives are typically used to make source programs easy to change and easy to compile in different execution environments. The mikroC PRO for ARM supports conditional compilation by replacing the appropriate source-code lines with a blank line.

All conditional compilation directives must be completed in the source or include file in which they have begun.

Directives #if, #elif, #else, and #endif

The conditional directives #if, #elif, #else, and #endif work very similar to the common C conditional statements. If the expression you write after #if has a nonzero value, the line group immediately following the #if directive is retained in the translation unit.

The syntax is:

#if constant_expression_1
<section_1>

[#elif constant_expression_2
<section_2>]
  ...
[#elif constant_expression_n
<section_n>]

[#else
<final_section>]

#endif

Each #if directive in a source file must be matched by a closing #endif directive. Any number of #elif directives can appear between #if and #endif directives, but at most one #else directive is allowed. The #else directive, if present, must be the last directive before #endif.

sections can be any program text that has meaning to compiler or preprocessor. The preprocessor selects a single section by evaluating constant_expression following each #if or #elif directive until it finds a true (nonzero) constant expression. The constant expressions are subject to macro expansion.

If all occurrences of constant-expression are false, or if no #elif directives appear, the preprocessor selects the text block after the #else clause. If the #else clause is omitted and all instances of constant_expression in the #if block are false, no section is selected for further processing.

Any processed section can contain further conditional clauses, nested to any depth. Each nested #else, #elif, or #endif directive belongs to the closest preceding the #if directive.

The net result of the preceding scenario is that only one code section (possibly empty) will be compiled.

Directives #ifdef and #ifndef

The #ifdef and #ifndef directives can be used anywhere #if can be used and they can test whether an identifier is currently defined or not. The line

#ifdef identifier

has exactly the same effect as #if 1 if identifier is currently defined, and the same effect as #if 0 if identifier is currently undefined. The other directive, #ifndef, tests true for the “not-defined” condition, producing the opposite results.

The syntax thereafter follows that of #if, #elif, #else, and #endif.

An identifier defined as NULL is considered to be defined.

Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code