 
    
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>#if Archives - Complete, Concrete, Concise</title>
	<atom:link href="https://complete-concrete-concise.com/tag/if/feed/" rel="self" type="application/rss+xml" />
	<link>https://complete-concrete-concise.com/tag/if/</link>
	<description>Practical Information Without The Bloat</description>
	<lastBuildDate>Wed, 13 Jul 2011 10:27:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>
	<item>
		<title>Preprocessor – the #if Directive</title>
		<link>https://complete-concrete-concise.com/programming/c/preprocessor-the-if-directive/</link>
		
		<dc:creator><![CDATA[richardsplanet]]></dc:creator>
		<pubDate>Wed, 13 Jul 2011 10:27:10 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[#if]]></category>
		<category><![CDATA[preprocessor]]></category>
		<category><![CDATA[understanding]]></category>
		<guid isPermaLink="false">http://complete-concrete-concise.com/?p=896</guid>

					<description><![CDATA[<p>This (along with the #elif directive) is probably the second most complicated preprocessor directive because the controlling expression can be complex and include tricky macro replacements. #if is one of five preprocessor selection statements allowing selection of alternative sections of code for compilation. The other four selection statements are: #ifdef, #ifndef, #elif, and #else. Behaviour [&#8230;]</p>
<p>The post <a href="https://complete-concrete-concise.com/programming/c/preprocessor-the-if-directive/">Preprocessor – the #if Directive</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="c1">
<p>This (along with the <code>#elif</code> directive) is probably the second most complicated preprocessor directive because the <em>controlling expression</em> can be complex and include tricky macro replacements.</p>
<p><code>#if</code> is one of five preprocessor selection statements allowing  selection of alternative sections of code for compilation. The other four selection statements are: <code>#ifdef</code>, <code>#ifndef</code>, <code>#elif</code>, and <code>#else</code>.</p>
<p>Behaviour of this preprocessor directive is the same for both C and C++ compilers.</p>
<p>An identifier is a sequence of letters, numbers and underscore characters. Identifiers must begin with a letter or underscore character. </p>
</div>
<h1>Purpose</h1>
<p>The <code>#if</code> directive controls whether the statements found between it and a terminating <code>#endif</code>, <code>#else</code>, or <code>#elif</code> directive are compiled or skipped. The decision is based on the value of the controlling expression.</p>
<h1>Format </h1>
<div class="c2">
<p><strong>#if</strong> <em>controlling_expression</em></p>
<p>preprocessor or code statements</p>
<p><strong>#elif</strong> or <strong>#else</strong> or <strong>#endif</strong></p>
</div>
<p>All preprocessor directives begin with the <code>#</code> symbol. It must be the first character on the line or the first character on the line following optional white space. </p>
<div class="c4">
<p>Some early compilers flagged an error if <code>#</code> was not the first character on the line.</p>
</div>
<p>Spaces or tabs are permitted between the <code>#</code> and <code>if</code>, but not escape characters or other symbols or macros. The preprocessor removes white space and concatenates the <code>#</code> and <code>if</code> together.</p>
<p>The following are valid uses:</p>
<pre>#if defined my_macro
# if defined my_macro;
# if     defined    my_macro
# /* comments are white space */ if defined my_macro</pre>
<p>The following are invalid uses:</p>
<pre>// #\ is not a valid preprocessor directive
# \t if defined my_macro
// #" is not a valid preprocessor directive
# "" if defined my_macro</pre>
<h1>Rules for the Controlling Expression</h1>
<p>The <em>controlling_expression</em> must be made up of: integers, identifiers, macros, character literals, operators, or the preprocessor operator <code>defined</code>. Strings, floating point values, preprocessor directives, the token pasting operator (##), and stringizing operator (#) are invalid and the program is malformed.</p>
<p>The <em>controlling_expression</em> must evaluate to an integer value.</p>
<p>If the <em>controlling expression</em> is non-zero (TRUE), then the statements following the <code>#if</code> until the end of the block (<code>#endif</code>, <code>#elif</code>, or <code>#else</code>) are compiled. </p>
<pre> #if 3
.
.
.
// because the controlling expression is non-zero (TRUE) everything
// until the end of the current block (#elif 42) will be compiled
.
.
.
#elif 42
.
.
.
// everything in here is skipped because the statements in the first
// block were compiled
.
.
.
#else
.
.
.
// all the statements here are skipped because the statements in the
// first block were compiled
.
.
.
#endif</pre>
<p>If the <em>controlling expression</em> is zero (FALSE), then the statements following the <code>#if</code> until the end of the block (<code>#endif</code>, <code>#elif</code>, or <code>#else</code>) are skipped. </p>
<p>Integers can be <code>signed</code> or <code>unsigned</code> and have a minimum range  equal to <code>long</code> and <code>unsigned long</code> integers, respectively.</p>
<p>Identifiers that are macro names are expanded and evaluated and their evaluation value replaces the macro name.</p>
<p>If the macro name is evaluated by the <code>defined</code> operator, then it is not expanded:</p>
<pre>#define MY_MACRO "twas brillig"
// MY_MACRO will never be expanded or evaluated.
// the defined operator determines if the identifier MY_MACRO
// exists. It returns 1 if the macro is defined or 0 if the macro is
// not defined.
#if defined MY_MACRO</pre>
<p>Identifiers that are not macro names, are replaced with a value of 0 (zero):</p>
<pre>#if SOME_RANDOM_IDENTIFIER</pre>
<p>If the identifier <code>SOME_RANDOM_IDENTIFIER</code> was never defined using <code>#define</code> or was undefined using <code>#undef</code>, then it is assigned the value of 0 (zero). To the preprocessor, it looks like: </p>
<pre>#if 0</pre>
<p>Character literals are converted into integer values. The value of a character literal appearing in either <code>#if</code> or <code>#elif</code> does not have to be the same as the value a character literal would have in a code expression. This is because the character set used by the preprocessor does not have to be the same as the character set used by the compiler.</p>
<p>Depending on the compiler, character literals may or may not be permitted to have negative values.</p>
<p>Any of the arithmetic, relational, logical, or bitwise operators may be used in the <em>controlling_expression</em>. Evaluation precedence is the same as for the language. </p>
<p>The controlling expression may be a simple or complex:</p>
<pre>// a simple controlling expression
#if    3</pre>
<pre>// a more complex controlling expression
#if ( MY_IDENTIFIER > 7 ) && ( VERSION >= 3 )</pre>
<p>Since the code is conditionally compiled, it is possible the code never gets compiled and, as a consequence, any errors in it are never caught or noticed.</p>
<p>Any valid language or preprocessor statements may occur within the selection block – even other <code>#if</code> statements. There is no restriction on the level of nesting permitted.</p>
<p>The following:</p>
<pre>#if defined MY_MACRO</pre>
<p>is equivalent to:</p>
<pre>#ifdef MY_MACRO</pre>
<p>The following:</p>
<pre>#if !defined MY_MACRO</pre>
<p>is equivalent to:</p>
<pre>#ifndef MY_MACRO</pre>
<p>The following:</p>
<pre>#if defined MY_MACRO && defined MY_OTHER_MACRO</pre>
<p>is equivalent to:</p>
<pre>#ifdef MY_MACRO
#  ifdef MY_OTHER_MACRO</pre>

<p>The post <a href="https://complete-concrete-concise.com/programming/c/preprocessor-the-if-directive/">Preprocessor – the #if Directive</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
