 
    
<?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>C++ Archives - Complete, Concrete, Concise</title>
	<atom:link href="https://complete-concrete-concise.com/category/programming/c-programming/feed/" rel="self" type="application/rss+xml" />
	<link>https://complete-concrete-concise.com/category/programming/c-programming/</link>
	<description>Practical Information Without The Bloat</description>
	<lastBuildDate>Tue, 19 Mar 2013 08:38:42 +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>Programming Error &#8211; Unintended String Concatenation</title>
		<link>https://complete-concrete-concise.com/programming/c/programming-error-unintended-string-concatenation/</link>
		
		<dc:creator><![CDATA[richardsplanet]]></dc:creator>
		<pubDate>Tue, 19 Mar 2013 08:38:42 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[comma]]></category>
		<category><![CDATA[concatenation]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[missing]]></category>
		<category><![CDATA[parser]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[programming]]></category>
		<guid isPermaLink="false">http://complete-concrete-concise.com/?p=2699</guid>

					<description><![CDATA[<p>In an effort to be helpful, C and C++ compilers concatenate adjacent strings together. In the following example, the compiler will concatenate the two strings into a single string: printf("This is a string " "and this is another string."); Which is probably what you intended anyway. Because C and C++ are free form languages, adjacent [&#8230;]</p>
<p>The post <a href="https://complete-concrete-concise.com/programming/c/programming-error-unintended-string-concatenation/">Programming Error &#8211; Unintended String Concatenation</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In an effort to be helpful, C and C++ compilers concatenate adjacent strings together.</p>
<p>In the following example, the compiler will concatenate the two strings into a single string:</p>
<pre><code>printf("This is a string " "and this is another string.");</code></pre>
<p>Which is probably what you intended anyway.</p>
<p>Because C and C++ are free form languages, adjacent strings can appear in many different formats &#8211; as this string table shows:</p>
<pre><code>char *array[] = {
    "str_1",
    "str_2",
    "str_3"
    "str_4",
    "str_5",
    NULL};</code></pre>
<p>Did you notice the missing comma (,) after <code>"str_3"</code>? It can be a hard one to spot.</p>
<p>This is the type of bug that can result in something like a simple parser failing:</p>
<pre><code>if (strcmp(array[i], test_string) == 0)</code></pre>
<p>because <code>str_3</code> and <code>str_4</code> will never be matched, but the others will.</p>

<p>The post <a href="https://complete-concrete-concise.com/programming/c/programming-error-unintended-string-concatenation/">Programming Error &#8211; Unintended String Concatenation</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Keyword &#8211; switch, case, default</title>
		<link>https://complete-concrete-concise.com/programming/c/keyword-switch-case-default/</link>
		
		<dc:creator><![CDATA[richardsplanet]]></dc:creator>
		<pubDate>Thu, 22 Mar 2012 06:56:52 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[break]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[case]]></category>
		<category><![CDATA[default]]></category>
		<category><![CDATA[keyword]]></category>
		<category><![CDATA[switch]]></category>
		<category><![CDATA[understanding]]></category>
		<guid isPermaLink="false">http://complete-concrete-concise.com/?p=1594</guid>

					<description><![CDATA[<p>The switch keyword is probably the least well understood of the C/C++ language keywords (although, const probably comes a close second). The keywords switch, case, and default always go together and cannot be used independently in any other context. There is a small difference in behaviour between C and C++. Most programmers will never notice [&#8230;]</p>
<p>The post <a href="https://complete-concrete-concise.com/programming/c/keyword-switch-case-default/">Keyword &#8211; switch, case, default</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="c1">
<p>The <code>switch</code> keyword is probably the least well understood of the C/C++ language keywords (although, <code>const</code> probably comes a close second).</p>
<p>The keywords <code>switch</code>, <code>case</code>, and <code>default</code> always go together and cannot be used independently in any other context.</p>
<p>There is a small difference in behaviour between C and C++. Most programmers will never notice the difference.</p>
</div>
<p>The most common use of <code>switch</code> is as a replacement for multiple <code>if-else</code> statements:</p>
<pre><code>
                               switch (value)
                               {
if (value == 1)                   case 1:
{                                      ...
  ...                                 break;
} else if (value == 2)            case 2:
{                                      ...
  ...                                 break;
} else                            default:
{                                      ...
  ...                                 break;
}                              }</code></pre>
<p>This usage is quite well understood. Its use as a jump table into a block of code is less well understood (and, to be honest, rarely used).</p>
<h1>Format</h1>
<pre><code><strong>switch</strong> ( <em>controlling expression</em> )
{
   <strong>case</strong>: <em>constant expression</em>  // optional
         .        .
         <em>code statements</em>
         .        .
         break;  // optional
   <strong>default</strong>:  //optional
         .        .
         <em>code statements</em>
         .        .
         break;  // optional
}</code></pre>
<h3>switch (<em>controlling expression</em>)</h3>
<p>Every <code>switch</code> block must begin with the <code>switch</code> keyword. If you want a <code>switch</code> block then you have to use the <code>switch</code> keyword.</p>
<p>C and C++ differ in the way the <em>controlling expression</em> is handled. </p>
<p><u><strong>In C</strong></u> the expression is converted to type <code>int</code> using C conversion rules. This means the <em>controlling expression</em> can be anything C knows how to convert to an <code>int</code>: the signed and unsigned varieties of <code>char</code>, <code>short</code>, <code>int</code>, <code>long </code> and <code>long long</code> as well as <code>float</code>, <code>double</code>, and <code>long double</code>.</p>
<p><u><strong>In C++</strong></u> the <em>controlling expression</em> must be an <u>integral</u> type &#8211; any of the signed or unsigned varieties of <code>char</code>, <code>short</code>, <code>int</code>, <code>long</code>, or <code>long long</code> &#8211; or any <code>class</code>, <code>struct</code>, or <code>union</code> for which there exists an unambiguous conversion to an integral type. Using a <code>float</code>, <code>double</code>, or <code>long double</code> is an error (unless, of course, you explicitly cast it to one of the integral types).</p>
<div class="c1">
<p>An &#8220;implicit&#8221; conversion from a <code>class</code>, <code>struct </code>or <code>union </code>to an integral type might seem new to some. Here is an example:</p>
<pre><code>class A
{
 private:
    int value;
 public:
    operator int() {return value;} // conversion method
};</code></pre>
<p>This can be used in a <code>switch</code> statement as follows:</p>
<pre><code>
int main(void)
{
   A a;  // variable of type A above with conversion to int
   switch (a) // works because compiler calls operator int()
   {
      .
      .
      .
   }
   return 0;
}</code></pre>
<p>In the above example, when an object of type A is used anywhere a type <code>int</code> is expected, the conversion operator <code>int()</code> will be called.</p>
<p>If we expand the class by adding another conversion operator to a different integral type, say</p>
<pre><code>operator char() {return char(value);}</code></pre>
<p>then it is no longer unambiguous because there are two integral types to choose from: <code>int</code> or <code>char</code>. The compile will fail because the compiler cannot resolve between the two integral types.</p>
<p>Conversions can be ambiguous in other types of code as well:</p>
<pre><code>A a;
int i;
char c;
short s;
i = a; // compiler calls operator int()
c = a; // compiler calls operator char();
s = a; // compiler cannot resolve between int() and char()</code></pre>
</div>
<h3>case (<em>constant expression</em>)</h3>
<div class="c1">
<p><code>case</code> statements are optional &#8211; you don&#8217;t have to have any. But, you do need at least one <code>case</code> or <code>default</code> statement in your <code>switch</code> body.</p>
</div>
<p>The <em>constant expression</em> is called a <u>label</u> or <u>case label</u>.</p>
<p>Unlike the <em>controlling expression</em>, the <code>case</code> label must be known at compile time &#8211; it cannot be a variable or in any other way determined at run time.</p>
<p>All <code>case</code> labels must be different. If two <code>case</code> labels resolve to the same value, then the program is malformed and the compile will fail.</p>
<div class="c1">
<p>The two most common ways that <code>case</code> labels get the same value is through macros or enums:</p>
<pre><code>
// at one time, the following macros all had different values
// but at some point the difference between CONDITION_1 and
// CONDITION_2 was lost and they became the same.
// Most code will work just fine with this change, but not a
// switch block if it is using these labels.
#define CONDITION_1 0
#define CONDITION_2 0  // duplicated value
#define CONDITION_3 2
// instead of using macros, the conditions were encapsulated
// in an enum, but at some point the difference between CONDITION_1
// and CONDITION_2 was lost and they became the same.
// Most code will work just fine with this change, but not a
// switch block if it is using these labels.
enum Some_Values
{
   CONDITION_1 = 0,
   CONDITION_2 = 0, // duplicated value
   CONDITION_3 = 1
};
// when either the macros or enum values are used for the case
// labels, the program becomes malformed because two labels
// have the same value
switch (value)
{
   case CONDITION_1:
   case CONDITION_2: // compile will fail
   case CONDITION_3:
   default:
}</code></pre>
</div>
<p>C and C++ differ in the way they handle the <em>constant expression</em>.</p>
<p><u><strong>In C</strong></u>, the the expression is converted to type <code>int</code>. Both the <em>controlling expression</em> and <em>constant expression</em> are of type <code>int</code>.</p>
<p><u><strong>In C++</strong></u>, the expression is converted to the type of the <em>controlling expression</em>. This means the <em>constant expression</em> is the same type as the <em>controlling expression</em>.</p>
<h3>default</h3>
<div class="c1">
<p>The <code>default</code> statement is optional &#8211; you don&#8217;t have to have one. But, you do need at least one <code>case</code> or <code>default</code> statement in your <code>switch</code> body.</p>
</div>
<p>The <code>default</code> statement is where the <code>switch</code> operator jumps to if none of the <code>case</code> labels match the <em>controlling expression</em>.</p>
<h1>Using switch for Multiway Selection</h1>
<div class="c1">
<p>This is the most common (and best understood) use of the <code>switch</code> statement.</div>
<p>The <code>switch</code> statement is most commonly used when you want to choose one of several code paths and the decision is based on some sort of integer value (an <code>enum</code> is an integer at heart).</p>
<p>For simple choices, an <code>if-else</code> block works fine. But when you have many possible code paths to choose from, having a long chain of <code>if-else</code> statements can be unwieldy.</p>
<p>Consider a basic DVD player that responds to the following commands:</p>
<pre><code>enum PLAYER_COMMANDS
{
   Off,
   On,
   Stop,
   Play,
   Pause,
   Eject
};</code></pre>
<p>Implemented using <code>if-else</code> statements, the code would look like this:</p>
<pre><code>if (command == Off)
{
   ... code ...
}
else if (command == On)
{
   ... code ...
}
... remaining else-if code ...
else
{
   printf("Error - unknown state\n");
}</code></pre>
<p>Using a <code>switch</code> statement, the code would look like this:</p>
<pre><code>switch (command)
{
   case Off:
      ... code ...;
      break;
   case On:
      ... code ...;
      break;
   ... remaining cases ...
   default:
      printf("Error - unknown state\n");
}</code></pre>
<p>The <code>switch</code> block looks neater and more compact than the nested <code>if-else</code> code.</p>
<h3>Order of case statements</h3>
<p>The <code>case</code> and <code>default</code> statements inside the <code>switch</code> don&#8217;t have to be in any particular order. While not typical, it is perfectly valid to have the <code>default</code> statement at the top of the block (or in the middle of the block):</p>
<pre><code>switch (controlling_expression)
{
   default: <em>// not typical location, but perfectly legal</em>
      ... some code ...
      break;
   case label_1:
      ... some code ...
     break;
};</code></pre>
<h3>Use of break</h3>
<p>Usually, a <code>break</code> statement is placed at just before the next <code>case</code> (or <code>default</code>) statement.</p>
<p>Code execution stops at the <code>break</code> statement and continues at the next statement following the <code>switch</code> block.</p>
<p>If there is no <code>break</code> statement, then the code continues to be executed until either (1) a <code>break</code> statement is encountered, or (2) execution exits the <code>switch</code> block.</p>
<pre><code>switch (controlling_expression)
{
   case label_1:
      ... some code 1 ...
   case label_2:
      ... some code 2 ...
      break;
   default:
      ... some code 3 ...
};</code></pre>
<p>In the above example, if the <em>controlling_expression</em> switches to:</p>
<ul>
<li><u>label_1</u>: then <em>some code 1</em> will be executed. Since there is no <code>break</code> statement, execution will fall through and execute <em>some code 2</em>. Since there is a <code>break</code> after <em>some code 2</em> code execution will continue at the next statement following the <code>switch</code> block.</li>
<li><u>label_2</u>: then <em>some code 2</em> will be executed. Since there is a <code>break</code> statement, code execution will continue at the next statement following the <code>switch</code> block.</li>
<li><u>default</u>: then <em>some code 3</em> will be executed. There is no <code>break</code> statement (it is optional for the last final label) because the next statement to be executed will be the one following the <code>switch</code> block.</li>
</ul>
<h3>Using Fall-through</h3>
<p>Fall-through can be useful.</p>
<p>Unlike some other languages, C and C++ don&#8217;t allow a range of values for for a <code>case</code> label. For example, you could not write:</p>
<pre><code>switch (month)
{
   case January ... July :
      ... some code ...
};</code></pre>
<p>But, using fall-through, you can write code like this:</p>
<pre><code>switch (month)
{
   case January:
   case March:
   case May:
   case July:
   case August:
   case October:
   case December:
      days = 31;
      break;
   case April:
   case June:
   case September:
   case November:
     days = 30;
     break;
   case February:
     days = 28;
};</code></pre>
<p>In the above example, January falls through to the assignment <code>days = 31;</code></p>
<h1>Using switch as a Jump Table</h1>
<div class="c1">
<p>This is the least commonly understood and used behaviour of the <code>switch</code> statement. Its most infamous example is <u>Duff&#8217;s Device</u>.</p>
</div>
<p>A common complaint of programmers coming from other languages is that the <code>switch</code> in C and C++ is broken &#8211; it seems dumb to have to explicitly code a <code>break</code> to prevent fall-through in <code>case</code> statements. That&#8217;s because in most languages a <code>switch</code> or <code>case</code> block is compact way of selecting a block of code to execute. This is not the case in C and C++. In C and C++, the <code>switch</code> statement is not for selecting a block of code to execute, it is a <u>jump table</u> <u>into</u> a block of code.</p>
<p>Consider the following:</p>
<pre><code>switch (control)
{
   default:
      printf ("Hello world!!!\n");
      printf ("Hello world!!!\n");
      printf ("Hello world!!!\n");
      printf ("Hello world!!!\n");
      printf ("Hello world!!!\n");
      printf ("Hello world!!!\n");
      printf ("Hello world!!!\n");
      printf ("Hello world!!!\n");
}</code></pre>
<p>No matter what value <code>control</code> has, the 8 <code>printf</code> statements will be executed. We might as well not even have the <code>switch</code> statement to begin with. The code is, effectively, identical to 8 <code>printf</code> statements enclosed in their own block:</p>
<pre><code>{
   printf ("Hello world!!!\n");
   printf ("Hello world!!!\n");
   printf ("Hello world!!!\n");
   printf ("Hello world!!!\n");
   printf ("Hello world!!!\n");
   printf ("Hello world!!!\n");
   printf ("Hello world!!!\n");
   printf ("Hello world!!!\n");
}</code></pre>
<p>We could also write it this way:</p>
<pre><code>goto DEFAULT: // whatever the value of control
{
DEFAULT:
      printf ("Hello world!!!\n");
      printf ("Hello world!!!\n");
      printf ("Hello world!!!\n");
      printf ("Hello world!!!\n");
      printf ("Hello world!!!\n");
      printf ("Hello world!!!\n");
      printf ("Hello world!!!\n");
      printf ("Hello world!!!\n");
}</code></pre>
<p>Consider the following (which will print Hello World!!! either 1, 2, 3, 4, 5, or 8 times depending on the value of <code>control</code>):</p>
<pre><code>switch (control)
{
   default:
      printf ("Hello world!!!\n");
      printf ("Hello world!!!\n");
      printf ("Hello world!!!\n");
   case 5:
      printf ("Hello world!!!\n");
   case 4:
      printf ("Hello world!!!\n");
   case 3:
      printf ("Hello world!!!\n");
   case 2:
      printf ("Hello world!!!\n");
   case 1:
      printf ("Hello world!!!\n");
}</code></pre>
<p>It can be rewritten as:</p>
<pre><code>if (control == 1) goto LABEL_1;
if (control == 2) goto LABEL_2;
if (control == 3) goto LABEL_3;
if (control == 4) goto LABEL_4;
if (control == 5) goto LABEL_5;
else goto DEFAULT;
{
DEFAULT:
      printf ("Hello world!!!\n");
      printf ("Hello world!!!\n");
      printf ("Hello world!!!\n");
LABEL_5:
      printf ("Hello world!!!\n");
LABEL_4:
      printf ("Hello world!!!\n");
LABEL_3:
      printf ("Hello world!!!\n");
LABEL_2:
      printf ("Hello world!!!\n");
LABEL_1:
      printf ("Hello world!!!\n");
}</code></pre>
<p>It should be obvious that a <code>switch</code> block is a constrained <code>goto</code> where you can jump to any line inside the block via an appropriate <code>case</code> label.</p>
<div class="c1">
<p>There is no need to get alarmed, <u>every</u> control statement in <u>every</u> programming language that alters the flow of program execution is a constrained goto.</p>
<p>Consider a <code>do-while</code> loop:</p>
<pre><code>do
{
   x = x + 2;
}
while (x &lt; 100);</code></pre>
<p>This is identical to:</p>
<pre><code>DO:
{
   x = x + 2;
}
if (x &lt; 100) goto DO;</code></pre>
</div>
<p>Knowing this helps to understand some of the &#8220;weird&#8221; behaviour of the <code>switch</code> statement in C and C++.</p>
<h3>Fall-through</h3>
<p>Fall through occurs because we <u>do not select</u> a block of code to execute, but <u>jump to a line of code</u> (within the block) and continue execution from there.</p>
<p>The <code>break</code> at the end of what we want to execute is simply a <code>goto</code> to the end of the block.</p>
<div class="c1">
<p>Looked at another way, <code>switch</code> or <code>case</code> constructs in other languages do exactly the same thing, except that before the next label, they implicitly insert a <code>goto</code> to the end of the block.</p>
<p>Consider the following example in Pascal:</p>
<pre><code> case A of
   0: writeln('nothing');
   1: writeln('number 1');
   2: writeln('number 2');
   else writeln('big number');
 end;</code></pre>
<p>This is identical to:</p>
<pre><code>if A = 0 then goto ZERO;
if A = 1 then goto ONE;
if A = 2 then goto TWO else goto BIG;
ZERO:
   writeln('nothing');
   goto CONTINUE;
ONE:
   1: writeln('number 1');
   goto CONTINUE;
TWO:
   2: writeln('number 2');
   goto CONTINUE;
BIG:
   writeln('big number');
CONTINUE:</code></pre>
<p>Notice the implicit <code>goto</code>s inserted to prevent fall-through.
</div>
<h3>Initialization Errors</h3>
<p>It is common to want to use some local variables in a <code>case</code> label. Unfortunately, doing so can generate a compile time error that the variable has not been initialized:</p>
<pre><code>switch (value)
{
   int a = 7;
   case 1:
       printf("%d\n", a);
       break;
   default:
       printf("%d\n", a * a);
}</code></pre>
<p>In this example, when the code jumps to <code>case 1</code> or <code>default</code> it bypasses the initialization of <code>a</code>.</p>
<h3>Jumping into the middle of and embedded switch</h3>
<p>Since a <code>switch</code> statement allows you to jump to an arbitrary point in a block of code, it is reasonable to ask if it is possible to jump into the middle of a nested <code>switch</code> block:</p>
<pre><code>switch (value)
{
   case LABEL_1:
      switch (value_2)
      {
         case OTHER_LABEL_1:
            ... code ...
            break;
         case LABEL_2:  // can <strong>switch (value)</strong> jump here?
            ... code ...
            break;
      }
   case LABEL_3:
      ... code ...
      break;
}</code></pre>
<p>The answer is <strong>no</strong>. This is because scope and visibility rules don&#8217;t allow <code>value</code> of the first <code>switch</code> to be seen inside of the nested <code>switch</code> block.</p>
<p>The following should make this clear:</p>
<pre><code>int A = 7; // this is the outer_A
{   // start a new block
   int A = 3; // this is the inner_A
  // inside this block, the value of the outer_A is not visible
  // (ok, in C++ you could use the :: scope resolution operator
  //  to see the outer_A, but that is not the point)
}</code></pre>
<p>In a similar way, the value of the <em>controlling expression</em> in the outer <code>switch</code> is hidden by the value of the <em>controlling expression</em> of the nested <code>switch</code>. This means the outer <em>controlling expression</em> has no scope within the nested <code>switch</code>, so it cannot jump into the middle of a nested <code>switch</code>.</p>

<p>The post <a href="https://complete-concrete-concise.com/programming/c/keyword-switch-case-default/">Keyword &#8211; switch, case, default</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to Add Comments to Macros</title>
		<link>https://complete-concrete-concise.com/programming/c/how-to-add-comments-to-macros/</link>
					<comments>https://complete-concrete-concise.com/programming/c/how-to-add-comments-to-macros/#comments</comments>
		
		<dc:creator><![CDATA[richardsplanet]]></dc:creator>
		<pubDate>Thu, 21 Jul 2011 01:34:12 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[comments]]></category>
		<category><![CDATA[macro]]></category>
		<category><![CDATA[preprocessor]]></category>
		<guid isPermaLink="false">http://complete-concrete-concise.com/?p=931</guid>

					<description><![CDATA[<p>Comments are an important part of documenting your code. Adding comments to macros is quite easy, but it has to be done the right way. This works with both C and C++ compilers. The easiest way to document macros is to just add comments before or after the macro definition: // returns the larger of [&#8230;]</p>
<p>The post <a href="https://complete-concrete-concise.com/programming/c/how-to-add-comments-to-macros/">How to Add Comments to Macros</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="c1">
<p>Comments are an important part of documenting your code.</p>
<p>Adding comments to macros is quite easy, but it has to be done the right way.</p>
<p>This works with both C and C++ compilers.</p>
</div>
<p>The easiest way to document macros is to just add comments before or after the macro definition:</p>
<pre>// returns the larger of the two arguments
#define MAX(x, y) (x)>(y)?(x):(y)</pre>
<p>This works well for small macros, but if you have a larger macro that is spread over several lines, it might be nice to put comments nearer some tricky or crucial bit of code. You can do that by using C-style comments (<code>/**/</code>). The preprocessor treats comments as white space, so comments of the form <code>/* some sort of comment */</code> are treated as white space.</p>
<pre>#define TRANS_TEST(a, b, c) \
{  <strong class="i2"> /* variables to store intermediate results*/</strong> \
    bool aGb = false;\
    bool bGc = false;\
    if (a > b) \
    {\
        aGb = true;
        printf("'a' is larger than 'b'\n");\
    }\
    if (b > c) \
    {\
        bGc = true;
        printf("'b' is larger than 'c'\n");\
    }\
    <strong class="i2">/* do the transitivity test */</strong> \
    if (aGb &&  bGc) \
    {\
        printf("by transitivity we know 'a' is larger than 'c'\n");\
    }\
}</pre>
<p>The above (contrived) example, determines if parameter <code>a</code> is greater than parameter <code>c</code> by using the principle of transitivity.</p>
<p>C style comments were used to document the code. Note the line splicing (<code>\</code>) operator appears at the end of each line.</p>
<p>When this code is compiled, the preprocessor will treat the comment (and any white space surrounding it) as white space.</p>
<p>NOTE: Using C++ single line comments (<code>//</code>)won&#8217;t work because everything following the <code>//</code> until the end of the line is considered white space. Even if the macro is <em>spread over several lines</em> using the line splicing (<code>\</code>) operator, the preprocessor turns it into a single line and everything following the <code>//</code> will be considered part of the comment (hence, white space).</p>
<pre>#define TRANS_TEST(a, b, c) \
{   <strong class="i2">// variables to store intermediate results</strong> \
    bool aGb = false;\
    bool bGc = false;\
    if (a > b) \
    {\
        aGb = true;
        printf("'a' is larger than 'b'\n");\
    }\
    if (b > c) \
    {\
       bGc = true;
       printf("'b' is larger than 'c'\n");\
    }\
    <strong class="i2">// do the transitivity test</strong>  \
    if (aGb &&  bGc) \
    {\
        printf("by transitivity we know 'a' is larger than 'c'\n");\
    }\
}</pre>
<p>In the above example, the C++ single line comment was used. When it is processed by the preprocessor, everything until the end of the line (after all the lines have been spliced together) will be considered a comment. Consequently, the compiler will issue an error because all it will see is:</p>
<pre><p>#define TRANS_TEST(a, b, c) {</p></pre>

<p>The post <a href="https://complete-concrete-concise.com/programming/c/how-to-add-comments-to-macros/">How to Add Comments to Macros</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://complete-concrete-concise.com/programming/c/how-to-add-comments-to-macros/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<item>
		<title>Preprocessor &#8211; the #error Directive</title>
		<link>https://complete-concrete-concise.com/programming/c/preprocessor-the-error-directive/</link>
		
		<dc:creator><![CDATA[richardsplanet]]></dc:creator>
		<pubDate>Wed, 20 Jul 2011 08:04:02 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[preprocessor]]></category>
		<category><![CDATA[understanding]]></category>
		<guid isPermaLink="false">http://complete-concrete-concise.com/programming/c/927</guid>

					<description><![CDATA[<p>Preprocessor &#8211; the #error Directive This is a very useful and often underused preprocessor directive. Behaviour of this preprocessor directive is the same for both C and C++ compilers. Purpose The #error directive terminates compilation and outputs the text following the directive. Format #error text All preprocessor directives begin with the # symbol. It must [&#8230;]</p>
<p>The post <a href="https://complete-concrete-concise.com/programming/c/preprocessor-the-error-directive/">Preprocessor &#8211; the #error Directive</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Preprocessor &#8211; the #error Directive</p>
<div class="c1">
<p>This is a very useful and often underused preprocessor directive.</p>
<p>Behaviour of this preprocessor directive is the same for both C and C++ compilers.</p>
</div>
<h1>Purpose</h1>
<p>The <code>#error</code> directive terminates compilation and outputs the text following the directive.</p>
<h1>Format </h1>
<pre><strong>#error</strong> <em>text</em></pre>
<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>error</code>, but not escape characters or other symbols or macros. The preprocessor removes white space and concatenates the <code>#</code> and <code>error</code> together.</p>
<p>If anything follows the <code>#error</code> directive (other than white space) then the program is malformed.</p>
<p>The following are valid uses:</p>
<pre>#error some error message text
# error some error text to display
# /* comments are white space */ error some error message to display</pre>
<p>The following are invalid uses:</p>
<pre>// #\ is not a valid preprocessor directive
# \t error text to output
// #" is not a valid preprocessor directive
# "" text to output</pre>
<h1>Use</h1>
<p>It is used to render a program malformed and output the text following the <code>#error</code> directive. The text may be quoted or unquoted (it doesn&#8217;t matter). No macro expansion takes place.</p>
<div class="c2">
<p>The language specifications do not say how the text following the <code>#error</code> directive is to be treated.</p>
<p>The GCC compiler, replaces all white space characters between tokens with a single white space character.</p>
<p>I have no reason to believe other compilers behave differently since white space is not considered significant in the C and C++ languages &#8211; it serves only to seperate tokens from one another.</p>
</div>
<p>There are many times when it is useful to halt compilation:</p>
<ol>
<li>code is incomplete</li>
<li>code requires particular library versions</li>
<li>code uses compiler dependent features</li>
<li>code has specific compiler requirements</li>
</ol>
<h2>Incomplete Code</h2>
<p>When developing code, it is common to create stub functions. For the final release, these stub functions need to be implemented. We can let the compiler help us catch unimplemented functions:</p>
<pre><p>int my_function( void )</p>
<p>{</p>
<p> #error my_function not implemented</p>
<p> return 0;</p>
<p>}</p></pre>
<p>The above code will fail for every compile. It might be more useful to allow compiling during development, but break the compile when we try to compile a release version. In the following example, we assume that during development, the macro <code>DEBUG</code> is defined:</p>
<pre><p>int my_function( void )</p>
<p>{</p>
<p>#ifndef DEBUG</p>
<p> #error my_function not implemented</p>
<p>#endif
<p> return 0;</p>
<p>}</p></pre>
<p>During development, we can compile the code, but when we do a release build (one in which <code>DEBUG</code> is not defined, then we catch unimplemented functions.</p>
<h2>Version Checking</h2>
<p>Sometimes code is dependent on particular versions of a library. It is useful to be able to stop compilation if an incorrect library version is included:</p>
<pre><p>#if library_version < 2</p>
<p>#error requires library_version 2 or better</p>
<p>#endif</p></pre>
<h2>Compiler Dependency</h2>
<p>Sometimes, code uses compiler specific features (for example, GCC allows nesting a <code>#define</code> within another <code>#define</code> &#8211; this is a non-standard compiler feature).</p>
<pre>#ifdef __GCC__
<p>// some GCC specific code goes here</p>
<p>#else</p>
<p>#error requires GCC compiler to compile</p>
<p>#endif</p></pre>
<h2>Compiler Requirements</h2>
<p>Sometimes code makes relies on certain assumptions about the target environment (for example, the size of an integer):</p>
<pre><p>#include <limits.h></p>
<p>#if UINT_MAX != 4294967295</p>
<p> #error this application requires 32 bit integers</p>
<p>#endif</p></pre>

<p>The post <a href="https://complete-concrete-concise.com/programming/c/preprocessor-the-error-directive/">Preprocessor &#8211; the #error Directive</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Preprocessor – the #line Directive</title>
		<link>https://complete-concrete-concise.com/programming/c/preprocessor-the-line-directive/</link>
					<comments>https://complete-concrete-concise.com/programming/c/preprocessor-the-line-directive/#comments</comments>
		
		<dc:creator><![CDATA[richardsplanet]]></dc:creator>
		<pubDate>Tue, 19 Jul 2011 13:46:30 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[#line]]></category>
		<category><![CDATA[preprocessor]]></category>
		<category><![CDATA[understanding]]></category>
		<guid isPermaLink="false">http://complete-concrete-concise.com/?p=925</guid>

					<description><![CDATA[<p>I don&#8217;t think I have ever seen this directive used. Behaviour of this preprocessor directive is the same for both C and C++ compilers. Purpose The #line directive allows setting the current line number and name of the file being compiled. Format #line integer or #line integer "file_name" or #line preprocessing_expression All preprocessor directives begin [&#8230;]</p>
<p>The post <a href="https://complete-concrete-concise.com/programming/c/preprocessor-the-line-directive/">Preprocessor – the #line 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>I don&#8217;t think I have ever seen this directive used.</p>
<p>Behaviour of this preprocessor directive is the same for both C and C++ compilers.</p>
</div>
<h1>Purpose</h1>
<p>The <code>#line</code> directive allows setting the current line number and  name of the file being compiled.</p>
<h1>Format </h1>
<pre><p><strong>#line</strong> <em>integer</em><p>
<p>or</p>
<p><strong>#line</strong> <em>integer</em> <em>"file_name"</em></p>
<p>or</p>
<p><strong>#line</strong> <em>preprocessing_expression</em></p></pre>
<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>line</code>, but not escape characters or other symbols or macros. The preprocessor removes white space and concatenates the <code>#</code> and <code>line</code> together.</p>
<p>If anything follows the <code>#line</code> directive (other than white space) then the program is malformed.</p>
<p>The following are valid uses:</p>
<pre>#line 100
# line   100
# /* comments are white space */ line 100</pre>
<p>The following are invalid uses:</p>
<pre>// #\ is not a valid preprocessor directive
# \t line 100
// #" is not a valid preprocessor directive
# "" line 100</pre>
<h1>Use</h1>
<p>There are three different forms of the <code>#line</code> directive which allow you to change the current line numbering and file name during preprocessing.</p>
<h2>First Form &#8211; #line <em>integer</em></h2>
<p>When the line number is changed, the line immediately <u>following</u> the <code>#line</code> directive receives the new line number:</p>
<pre>#line 123
/* this line is now line number 123 */
/* this line is line number 124 */
/* this line is line number 126 */</pre>
<div class="c2">
<p>For C89/C90 and C++97, the valid range of values for the line number is from 1 to 32767 (0x0001 to 0x7FFF).</p>
<p>In C99 and the upcoming C++0x, the valid range of values for the line number is from 1 to 2147483648 (0x00000001 to 0x7FFFFFFF).</p>
<p>How the compiler deals with out of range values is undefined. Some compilers may accept them, others may issue a warning or error.</p>
</div>
<h2>Second Form &#8211; #line <em>integer</em> <em>file_name</em></h2>
<p>When <em>file_name</em> is specified, the working name of the file being compiled is changed to the specified string. The <em>file_name</em> must be enclosed by double quotes (<code>"</code>).</p>
<p>In the following example, the line number remains the same but the name of the file being compiled is changed to <code>new_file_name.abc</code>. Only the name the compiler thinks it is compiling changes (and which it will return using the <code>__FILE__</code> macro), the actual (physical) file name remains the same:</p>
<pre><p>#line __LINE__ "new_file_name.abc"</p></pre>
<h2>Third Form &#8211; #line <em>preprocessor_tokens</em></h2>
<p>The final form of the <code>#line</code> directive allows you to specify  preprocessor tokens to be expanded. After expansion, the third form must look like one of the first two forms. If it does not, the program is malformed. <strong>NOTE:</strong> the preprocessor tokens are expanded, but not evaluated.</p>
<pre><p>#define LINE_NUMBER 100</p>
<p>#define FILE_NAME(f) #f</p>
<p />
<p>#line LINE_NUMBER FILE_NAME(abc.h)</p></pre>
<p>In the above example, <code>LINE_NUMBER</code> is expanded to 100 and <code>FILE_NAME</code> stringizes the passed parameter.</p>
<p>After compilation, the next line will be line number <code>100</code> and the name of the file being compiled will be <code>abc.h</code>.</p>
<div class="c2">
<p><strong>NOTE:</strong> changing the line numbers and file names can cause grief when you try to debug your code.</p>
<p>Changing line numbers and the working file name can make debugging much harder.</p>
<p>When I used GCC to test the code, the debug information contained  line numbering information and file name information that was different from what the IDE (in this case Code::Blocks) knew it to be. When I set breakpoints, Code::Blocks used the line and file name information it was aware of &#8211; which was different from what ended up in the debug information. I had to set the breakpoints manually for the &#8220;correct&#8221; line numbers and file name.</p>
</div>

<p>The post <a href="https://complete-concrete-concise.com/programming/c/preprocessor-the-line-directive/">Preprocessor – the #line Directive</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://complete-concrete-concise.com/programming/c/preprocessor-the-line-directive/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Preprocessor – the #pragma Directive</title>
		<link>https://complete-concrete-concise.com/programming/c/preprocessor-the-pragma-directive/</link>
		
		<dc:creator><![CDATA[richardsplanet]]></dc:creator>
		<pubDate>Sun, 17 Jul 2011 19:07:22 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[#pragma]]></category>
		<category><![CDATA[preprocessor]]></category>
		<category><![CDATA[understanding]]></category>
		<guid isPermaLink="false">http://complete-concrete-concise.com/?p=909</guid>

					<description><![CDATA[<p>Behaviour of this preprocessor directive is the same for both C and C++ compilers. Behaviour of this directive is very likely to vary from one compiler vendor to another. Purpose The #pragma directive allows implementation specific control of the compiler. Format #pragma command All preprocessor directives begin with the # symbol. It must be the [&#8230;]</p>
<p>The post <a href="https://complete-concrete-concise.com/programming/c/preprocessor-the-pragma-directive/">Preprocessor – the #pragma 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>Behaviour of this preprocessor directive is the same for both C and C++ compilers.<br />
Behaviour of this directive is very likely to vary from one compiler vendor to another.</p>
</div>
<h1>Purpose</h1>
<p>The <code>#pragma</code> directive allows implementation specific control of the compiler.</p>
<h1>Format </h1>
<div class="c2">
<p><strong>#pragma</strong> <em>command</em></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>pragma</code>, but not escape characters or other symbols or macros. The preprocessor removes white space and concatenates the <code>#</code> and <code>pragma</code> together.</p>
<p>If anything follows the <code>#pragma</code> directive (other than white space) then the program is malformed.</p>
<p>The following are valid uses:</p>
<pre>#pragma pack(2)
# pragma once
# /* comments are white space */ pragma long_calls</pre>
<p>The following are invalid uses:</p>
<pre>// #\ is not a valid preprocessor directive
# \t pragma pack(2)
// #" is not a valid preprocessor directive
# "" pragma once</pre>
<h1>Use</h1>
<p>All <code>#pragma</code> commands are compiler specific, so you need to consult your compiler documentation for details.</p>
<p>If the <code>#pragma <em>command</em></code> is not recognized, the preprocessor ignores it.</p>
<p>Pragmas are used to pass information to the compiler that cannot be done through the language.</p>
<p>A common use of pragmas is to specify the packing / alignment of data. Normally, a compiler will align data on the processor&#8217;s natural boundary (which is usually the same as the processor&#8217;s bit size). However, for structures with byte defined data, 8-bit alignment may be required. The following shows byte alignment packing using Microsoft C/C++:</p>
<pre>#pragma pack(push) /* save the current packing value */
#pragma pack(1) /* pack on a 1 byte boundary */
struct some_type_with_8_bit_alignment
{
char a;
short b;
long c;
char d;
};
#pragma pack(pop) /* restore the saved packing value */</pre>

<p>The post <a href="https://complete-concrete-concise.com/programming/c/preprocessor-the-pragma-directive/">Preprocessor – the #pragma Directive</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Preprocessor – Understanding the stringizing (#) Operator</title>
		<link>https://complete-concrete-concise.com/programming/c/preprocessor-understanding-the-stringizing-operator/</link>
		
		<dc:creator><![CDATA[richardsplanet]]></dc:creator>
		<pubDate>Sat, 16 Jul 2011 19:47:33 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[#]]></category>
		<category><![CDATA[operator]]></category>
		<category><![CDATA[preprocessor]]></category>
		<category><![CDATA[stringizing]]></category>
		<category><![CDATA[understanding]]></category>
		<guid isPermaLink="false">http://complete-concrete-concise.com/?p=908</guid>

					<description><![CDATA[<p>This is one of three preprocessor operators. The other two operators are the token pasting operator (##) and the define operator. The behaviour is the same for both C and C++ compilers. Purpose The stringizing operator is used to convert parameters passed to a macro into strings. Format # token_to_turn_into_a_string Use The stringizing (#) operator [&#8230;]</p>
<p>The post <a href="https://complete-concrete-concise.com/programming/c/preprocessor-understanding-the-stringizing-operator/">Preprocessor – Understanding the stringizing (#) Operator</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 is one of three preprocessor operators. The other two operators are the token pasting operator (<code>##</code>) and the <code>define</code> operator.</p>
<p>The behaviour is the same for both C and C++ compilers.</p>
</div>
<h1>Purpose</h1>
<p>The stringizing operator is used to convert parameters passed to a macro into strings.</p>
<h1>Format</h1>
<p><strong>#</strong> <em>token_to_turn_into_a_string</em></p>
<h1>Use</h1>
<p>The stringizing (<code>#</code>) operator may only be used with <code>#define</code> preprocessor directives taking a parameter.</p>
<p>The parameter token immediately to the right of the stringizing operator (ignoring any white space) is transformed by the preprocessor into a string.</p>
<p>For example:</code></p>
<pre>#define make_string(x) #x</pre>
<p>will convert whatever is passed as parameter <code>x</code> into a string.</p>
<p>Leading and trailing white space are deleted.</p>
<p>Any white space between the parameter&#8217;s tokens are collapsed into a single white space character between tokens.</p>
<p>Any conversions necessary to make character literals in the parameter&#8217;s tokens valid in a string are automatically made. This means that characters like: <code>\</code> and <code>"</code> are automatically converted to <code>\\</code> and <code>\"</code> respectively.</p>
<p>If the token is a macro, the macro is not expanded &#8211; the macro name is converted into a string. As a consequence, some characters cannot be stringized – notably, the comma (<code>,</code>) because it is used to delimit parameters and the right parenthesis (<code>)</code>) because it marks the end of the parameter list.</p>
<h2>Examples</h2>
<p>Given the following macros:</p>
<pre><p>#define make_string(x) #x</p>
<p>#define COMMA ,</p></pre>
<p>the following uses:</p>
<pre>make_string(twas brillig);
make_string( twas brillig );
make_string("twas" "brillig");
make_string(twas COMMA brillig);</pre>
<p>get expanded into:</p>
<pre>// no surprise here
"twas brillig"
// leading and trailing spaces were trimmed,
// space between words was compressed to a single space character
"twas brillig"
// the quotes were automatically converted
"\"twas\" \"brillig\""
// the macro COMMA was not expanded
"twas COMMA brillig"</pre>
<div class="c1">
<p>In C99 and the upcoming C++0x languages, you can use a variadic macro to pass commas to the stringizing operator.</p>
<pre>#define make_string(...) # __VA_ARGS__</pre>
<p>Since __VA_ARGS__ does not process the comma separator, you can pass it to the stringizing operator. However, you still cannot pass the right parenthesis.</p>
</div>
<h1>Stringizing a Single Character</h1>
<p>Sometimes you want to create a character literal. Unfortunately, the language does not specify a <em>charizing</em> operator (although, the Microsoft compiler does provide the non-standard operator <code>#@</code> for charizing a single character).</p>
<p>However, you can simulate a <em>charizing</em> operator as follows:</p>
<pre>#define CHARIZE(x) #x[0]</pre>
<p>What this does is stringize whatever is passed as parameter <code>x</code> and then addresses the first character &#8211; effectively acting as though the parameter had been <em>charized</em>. This works in most places a character literal is expected, but not all.</p>

<p>The post <a href="https://complete-concrete-concise.com/programming/c/preprocessor-understanding-the-stringizing-operator/">Preprocessor – Understanding the stringizing (#) Operator</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Preprocessor – the #endif Directive</title>
		<link>https://complete-concrete-concise.com/programming/c/preprocessor-the-endif-directive/</link>
		
		<dc:creator><![CDATA[richardsplanet]]></dc:creator>
		<pubDate>Fri, 15 Jul 2011 22:40:58 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[#endif]]></category>
		<category><![CDATA[preprocessor]]></category>
		<category><![CDATA[understanding]]></category>
		<guid isPermaLink="false">http://complete-concrete-concise.com/programming/c/preprocessor-%e2%80%93-the-endif-directive</guid>

					<description><![CDATA[<p>Behaviour of this preprocessor directive is the same for both C and C++ compilers. Purpose The #endif directive is used end / close / terminate a selection block (#if, #ifdef, or #ifndef. Format #if or #ifdef or #ifndef preprocessor or code statements #elif controlling_expression (optional) preprocessor or code statements #else (optional) preprocessor or code statements [&#8230;]</p>
<p>The post <a href="https://complete-concrete-concise.com/programming/c/preprocessor-the-endif-directive/">Preprocessor – the #endif 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>Behaviour of this preprocessor directive is the same for both C and C++ compilers.</p>
</div>
<h1>Purpose</h1>
<p>The <code>#endif</code> directive is used end / close / terminate a selection block (<code>#if</code>, <code>#ifdef</code>, or <code>#ifndef</code>.</p>
<h1>Format </h1>
<div class="c2">
<p><strong>#if</strong> or <strong>#ifdef</strong> or <strong>#ifndef</strong></p>
<p>preprocessor or code statements</p>
<p><strong>#elif</strong> <em>controlling_expression</em> (optional)</p>
<p>preprocessor or code statements</p>
<p> <strong>#else</strong> (optional)</p>
<p>preprocessor or code statements</p>
<p><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>endif</code>, but not escape characters or other symbols or macros. The preprocessor removes white space and concatenates the <code>#</code> and <code>endif</code> together.</p>
<p>If anything follows the <code>#endif</code> directive (other than white space) then the program is malformed.</p>
<p>The following are valid uses:</p>
<pre>#endif
#   endif
# /* comments are white space */ endif</pre>
<p>The following are invalid uses:</p>
<pre>// #\ is not a valid preprocessor directive
# \t endif
// #" is not a valid preprocessor directive
# "" endif
// malformed because only white space may follow #endif
#endif MY_MACRO</pre>
<h1>Use</h1>
<p>The <code>#endif</code> must appear as the final statement of a preprocessor selection sequence.</p>
<pre><p>#ifdef MY_MACRO</p>
.
.
.
// preprocessor or language statements
.
.
.
<p>#endif</p></pre>

<p>The post <a href="https://complete-concrete-concise.com/programming/c/preprocessor-the-endif-directive/">Preprocessor – the #endif Directive</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Preprocessor – the #else Directive</title>
		<link>https://complete-concrete-concise.com/programming/c/preprocessor-the-else-directive/</link>
		
		<dc:creator><![CDATA[richardsplanet]]></dc:creator>
		<pubDate>Wed, 13 Jul 2011 17:39:51 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[#else]]></category>
		<category><![CDATA[preprocessor]]></category>
		<category><![CDATA[understanding]]></category>
		<guid isPermaLink="false">http://complete-concrete-concise.com/?p=898</guid>

					<description><![CDATA[<p>#else is one of five preprocessor selection statements allowing selection of alternative sections of code for compilation. The other four selection statements are: #ifdef, #ifndef, #if, and #elif. Behaviour of this preprocessor directive is the same for both C and C++ compilers. Purpose The #else directive provides a final alternative for a preprocessor selection block. [&#8230;]</p>
<p>The post <a href="https://complete-concrete-concise.com/programming/c/preprocessor-the-else-directive/">Preprocessor – the #else 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><code>#else</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>#if</code>, and <code>#elif</code>.</p>
<p>Behaviour of this preprocessor directive is the same for both C and C++ compilers.</p>
</div>
<h1>Purpose</h1>
<p>The <code>#else</code> directive provides a final alternative for a preprocessor selection block. If all previous selection options fail, then the code found between the <code>#else</code> and <code>#endif</code> is compiled.</p>
<h1>Format </h1>
<div class="c2">
<p><strong>#if</strong> or <strong>#ifdef</strong> or <strong>#ifndef</strong></p>
<p>preprocessor or code statements</p>
<p><strong>#elif</strong> <em>controlling_expression</em> (optional)</p>
<p>preprocessor or code statements</p>
<p> <strong>#else</strong></p>
<p>preprocessor or code statements</p>
<p><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>else</code>, but not escape characters or other symbols or macros. The preprocessor removes white space and concatenates the <code>#</code> and <code>else</code> together.</p>
<p>If anything follows the <code>#else</code> directive (other than white space) then the program is malformed.</p>
<p>The following are valid uses:</p>
<pre>#else
#   else
# /* comments are white space */ else</pre>
<p>The following are invalid uses:</p>
<pre>// #\ is not a valid preprocessor directive
# \t else
// #" is not a valid preprocessor directive
# "" else
// malformed because only white space may follow #else
#else MY_MACRO</pre>
<h1>Use</h1>
<p>The <code>#else</code> must appear as the last alternative in a preprocessor selection block. If all the preceding selection options fail, then the code contained between the <code>#else</code> and closing <code>#endif</code> is compiled.</p>
<p>The statements following the <code>#else</code> may be language statements or preprocessor statements. There is no limit on the nesting of preprocessor directives or statements.</p>
<pre> #ifdef MY_MACRO
.
.
.
// these statements will only be compiled if MY_MACRO exists
.
.
.
#else
.
.
.
// these statements will only be compiled if MY_MACRO does not exist
.
.
.
#endif</pre>

<p>The post <a href="https://complete-concrete-concise.com/programming/c/preprocessor-the-else-directive/">Preprocessor – the #else Directive</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Preprocessor &#8211; the #ifndef Directive</title>
		<link>https://complete-concrete-concise.com/programming/c/preprocessor-the-ifndef-directive/</link>
		
		<dc:creator><![CDATA[richardsplanet]]></dc:creator>
		<pubDate>Wed, 13 Jul 2011 08:36:18 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[#ifndef]]></category>
		<category><![CDATA[preprocessor]]></category>
		<category><![CDATA[understanding]]></category>
		<guid isPermaLink="false">http://complete-concrete-concise.com/?p=888</guid>

					<description><![CDATA[<p>Behaviour of the #ifndef directive is the same in both C and C++. Purpose The #ifndef directive is one of five preprocessor selection statements allowing selection of alternative sections of code for compilation. The other four selection statements are: #ifdef, #if, #elif, and #else. Format #ifndef macro name valid preprocessor or code statements #endif or [&#8230;]</p>
<p>The post <a href="https://complete-concrete-concise.com/programming/c/preprocessor-the-ifndef-directive/">Preprocessor &#8211; the #ifndef 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>Behaviour of the <code>#ifndef</code> directive is the same in both C and C++.</p>
</div>
<h1>Purpose</h1>
<p>The <code>#ifndef</code> directive 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>#if</code>, <code>#elif</code>, and <code>#else</code>.</p>
<h1>Format </h1>
<div class="c2">
<p><strong>#ifndef</strong> <em>macro name</em></p>
<p>valid preprocessor or code statements</p>
<p><strong>#endif</strong> or <strong>#elif</strong> or <strong>#else</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>ifndef</code>, but not escape characters or other symbols or macros. The preprocessor removes white space and concatenates the <code>#</code> and <code>ifndef</code> together.</p>
<p>The following are valid uses:</p>
<pre>#ifndef my_macro
# ifndef my_macro;
# ifndef my_macro
# /* comments are white space */ ifndef my_macro</pre>
<p>The following are invalid uses:</p>
<pre>// #\ is not a valid preprocessor directive
# \t ifndef my_macro
// #" is not a valid preprocessor directive
# "" ifndef my_macro</pre>
<h1>Use</h1>
<p>If the macro name does not exist, then the statements following <code>#ifndef</code> until the end of the block (<code>#endif</code>, <code>#else</code> or <code>#elif</code>) are compiled.</p>
<p>If the macro name exists, then the statements following <code>#ifndef</code> until the end of the block (<code>#endif</code>, <code>#else</code> or <code>#elif</code>) are skipped (not compiled).</p>
<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>The simplest preprocessor selection block consists of just an <code>#ifndef</code> and a terminating <code>#endif</code> statement.</p>
<p>More complex selection blocks consist of an <code>#ifndef</code> followed by one or more <code>#elif</code> and / or a final <code>#else</code> statement.</p>
<p>Any valid preprocessor statements, including other <code>#ifndef</code> statements, may be part of the code in the selection block. There is no limit on the level of nesting of selection statements. </p>
<p>The following:</p>
<pre>#ifndef my_macro</pre>
<p>is equivalent to:</p>
<pre>#if !defined my_macro</pre>
<p>This directive is most commonly used to prevent multiple inclusion of header files:</p>
<pre>#ifndef my_header
#define my_header
.
.
.
// contents of the header file
.
.
.
#endif</pre>

<p>The post <a href="https://complete-concrete-concise.com/programming/c/preprocessor-the-ifndef-directive/">Preprocessor &#8211; the #ifndef Directive</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Preprocessor – the #elif Directive</title>
		<link>https://complete-concrete-concise.com/programming/c/preprocessor-the-elif-directive/</link>
		
		<dc:creator><![CDATA[richardsplanet]]></dc:creator>
		<pubDate>Wed, 13 Jul 2011 10:31:26 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[#elif]]></category>
		<category><![CDATA[preprocessor]]></category>
		<category><![CDATA[understanding]]></category>
		<guid isPermaLink="false">http://complete-concrete-concise.com/?p=897</guid>

					<description><![CDATA[<p>This (along with the #if directive) is probably the second most complicated preprocessor directive because the controlling expression can be complex and include tricky macro replacements. #elif is one of five preprocessor selection statements allowing selection of alternative sections of code for compilation. The other four selection statements are: #ifdef, #ifndef, #if, and #else. Behaviour [&#8230;]</p>
<p>The post <a href="https://complete-concrete-concise.com/programming/c/preprocessor-the-elif-directive/">Preprocessor – the #elif 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>#if</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>#elif</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>#if</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>#elif</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>
<p>It is used to allow creating more complex code selection blocks, featuring more alternatives.</p>
<h1>Format </h1>
<div class="c2">
<p><strong>#if</strong> or <strong>#ifdef</strong> or <strong>#ifndef</strong></p>
<p>preprocessor or code statements</p>
<p><strong>#elif</strong> <em>controlling_expression</em></p>
<p>preprocessor or code statements</p>
<p><strong>#endif</strong>, <strong>#elif</strong>, or <strong>#else</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>elif</code>, but not escape characters or other symbols or macros. The preprocessor removes white space and concatenates the <code>#</code> and <code>elif</code> together.</p>
<p>The following are valid uses:</p>
<pre>#elif defined my_macro
# elif defined my_macro;
# elif defined my_macro
# /* comments are white space */ elif defined my_macro</pre>
<p>The following are invalid uses:</p>
<pre>// #\ is not a valid preprocessor directive
# \t elif defined my_macro
// #" is not a valid preprocessor directive
# "" elif 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>#elif</code> until the end of the block (<code>#endif</code>, <code>#elif</code>, or <code>#else</code>) are compiled. </p>
<pre> #if 0
.
.
.
// because the controlling expression is zero (FALSE) everything
// until the end of the current block (#elif 42) will be skipped
.
.
.
#elif 42
.
.
.
// because everything in the first block was skipped, and this
// controlling expression evaluates to non-zero (TRUE), all the
// statements until the end of the current block (#else) will be
// compiled.
.
.
.
#else
.
.
.
// all the statements here are skipped because the statements in
// the previous block were compiled
.
.
.
#endif</pre>
<p>If the <em>controlling expression</em> is zero (FALSE), then the statements following the <code>#elif</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 0
// everything in here is skipped
#elif defined MY_MACRO
// code in here will only be compiled if MY_MACRO exists
#endif</pre>
<p>Identifiers that are not macro names, are replaced with a value of 0 (zero):</p>
<pre>#elif SOME_RANDOM_IDENTIFIER</pre>
<p>If the identifier <code>SOME_RANDOM_IDENTIFIER</code> was never defined using <code>#define</code> or was undefined (before its use) using <code>#undef</code>, then it is assigned the value of 0 (zero). To the preprocessor, it looks like: </p>
<pre>#elif 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><code>#elif</code> never begins a selection block, it is always used as an alternative selection following a previous <code>#if</code>, <code>#ifdef</code>, <code>#ifndef</code>, or <code>#elif</code> preprocessor directive.</p>

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