 
    
<?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>macro Archives - Complete, Concrete, Concise</title>
	<atom:link href="https://complete-concrete-concise.com/tag/macro/feed/" rel="self" type="application/rss+xml" />
	<link>https://complete-concrete-concise.com/tag/macro/</link>
	<description>Practical Information Without The Bloat</description>
	<lastBuildDate>Thu, 21 Jul 2011 01:34:12 +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>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 #undef Directive</title>
		<link>https://complete-concrete-concise.com/programming/c/preprocessor-the-undef-directive/</link>
		
		<dc:creator><![CDATA[richardsplanet]]></dc:creator>
		<pubDate>Sun, 19 Jun 2011 17:34:42 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[macro]]></category>
		<category><![CDATA[preprocessor]]></category>
		<category><![CDATA[undef]]></category>
		<category><![CDATA[understanding]]></category>
		<guid isPermaLink="false">http://complete-concrete-concise.com/programming/c/preprocessor-the-undef-directive</guid>

					<description><![CDATA[<p>Behaviour of the #undef directive is the same in both C and C++. Purpose It is used to undefine a macro. A macro is an identifier (or label) followed by replacement text. There is only a single namespace for macros. A program which redefines an existing macro is considered to be malformed &#8211; even though [&#8230;]</p>
<p>The post <a href="https://complete-concrete-concise.com/programming/c/preprocessor-the-undef-directive/">Preprocessor &#8211; the #undef 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>#undef</code> directive is the same in both C and C++.</p>
</div>
<h1>Purpose</h1>
<p>It is used to undefine a macro. </p>
<p>A macro is an identifier (or label) followed by replacement text.</p>
<p>There is only a single namespace for macros. A program which redefines an existing macro is considered to be malformed &#8211; even though most compilers only generate a warning instead of an error.</p>
<h1>Format</h1>
<div class="c2"><code><strong>#undef</strong> <em>MACRO_NAME </em></code></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>undef</code>, but not escape characters or other symbols or macros. The preprocessor removes whitespace and concatenates the <code>#</code> and <code>undef</code> together.</p>
<p>The following are valid uses:</p>
<pre>#undef my_macro
#    undef      my_macro
     # undef my_macro
# /* comments are whitespace */ undef my_macro</pre>
<p>The following are invalid uses:</p>
<pre>#define empty_macro
// #empty_macro is not a valid preprocessor directive
#   empty_macro undef my_macro
// #\ is not a valid preprocessor directive
# \t undef my_macro
// #" is not a valid preprocessor directive
# "" undef my_macro</pre>
<p>If the specified macro name does not exist, nothing happens.</p>
<p>If the specified macro name exists, its name, parameter list, and replacement text is removed.</p>
<p>Conceptually, macros are stored in a 3 column table:</p>
<pre>|------------|----------------|------------------|
| <strong>Macro Name</strong> | <strong>Parameter List</strong> | <strong>Replacement Text</strong> |
|------------|----------------|------------------|
| my_macro   |                | // some text     |
|------------|----------------|------------------|
| macro_2a   | a, b           | ((a)*(b))        |
|------------|----------------|------------------|</pre>
<p>When we <code>#undef</code> a macro, we remove it from the table.</p>
<p>A program is malformed if anything other than whitespace (comments count as whitespace) and a newline character follow the macro name.</p>

<p>The post <a href="https://complete-concrete-concise.com/programming/c/preprocessor-the-undef-directive/">Preprocessor &#8211; the #undef 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 #define Directive</title>
		<link>https://complete-concrete-concise.com/programming/c/preprocessor-the-define-directive/</link>
		
		<dc:creator><![CDATA[richardsplanet]]></dc:creator>
		<pubDate>Sun, 19 Jun 2011 02:33:08 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[#define]]></category>
		<category><![CDATA[macro]]></category>
		<category><![CDATA[macros]]></category>
		<category><![CDATA[preprocessor]]></category>
		<category><![CDATA[understanding]]></category>
		<category><![CDATA[variadic]]></category>
		<guid isPermaLink="false">http://complete-concrete-concise.com/?p=788</guid>

					<description><![CDATA[<p>This is the most complicated of the preprocessor directives (a little over 7 pages is devoted to it in the C99 spec and the C++98 spec devotes about 4 pages to it). Behaviour of the #define directive is the same in both C and C++. C99 added variable argument macros; the upcoming C++0x standard adds [&#8230;]</p>
<p>The post <a href="https://complete-concrete-concise.com/programming/c/preprocessor-the-define-directive/">Preprocessor &#8211; The #define 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 is the most complicated of the preprocessor directives (a little over 7 pages is devoted to it in the C99 spec and the C++98 spec devotes about 4 pages to it).</p>
<p>Behaviour of the <code>#define</code> directive is the same in both C and C++.</p>
<p>C99 added variable argument macros; the upcoming C++0x standard adds variadic macros, as well.</p></div>
<h1>Purpose</h1>
<p>It is used to define a macro. </p>
<p>A macro is an identifier (or label) followed by text.</p>
<p>When the preprocessor encounters the macro in the program it replaces the macro with the text that follows the macro name. </p>
<p>There is no restriction on what may be in the replacement text. The only restriction on the replacement text is that it must be all on the same line as the <code>#define</code> directive (you can use the line continuation character <code>\</code> to spread your macro over several lines).</p>
<p>A macro may be a simple label or it may be a label with a parameter list. Macros also may use the token pasting (##) operator and stringizing (#) operator to modify their replacement text.</p>
<h1>Format</h1>
<div class="c2"><code><strong>#define</strong> <em>MACRO_NAME (optional_parameter_list) replacement_text</em></code></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>define</code>, but not escape characters or other symbols or macros. The preprocessor removes whitespace and concatenates the <code>#</code> and <code>define</code> together.</p>
<p>The following are valid uses:</p>
<pre>#define my_macro
# define my_macro
#   define     my_macro</pre>
<p>The following are invalid uses:</p>
<pre>#define empty_macro
// #empty_macro is not a valid preprocessor directive
# empty_macro define my_macro
// #\ is not a valid preprocessor directive
# \t define my_macro
// #" is not a valid preprocessor directive
# "" define my_macro</pre>
<h1>Macros with no Parameters</h1>
<p>The simplest macros are those which only have a name and some replacement text:</p>
<pre>#define my_macro
#define my_other_macro "This is some replacement text."</pre>
<p>The first <code>#define</code> creates a macro named <code>my_macro</code> that has no replacement text. This is valid because the replacement text is just nothing (effectively an empty string).</p>
<p>The second <code>define</code> creates a macro named <code>my_other_macro</code> that has <code>"This is some replacement text"</code> as its replacement text (including the quotes).</p>
<p>The replacement text begins at the first non-whitespace character following the macro name and ends at the last non-whitespace character on the line (remember, you can spread your macro over multiple lines by using the <code>\</code> operator to indicate line continuation).</p>
<pre>
#define my_macro This is some replacement text
<strong>                 ^                           ^
The replacement text begins and ends at the ^ characters.</strong></pre>
<p>Every time the preprocessor encounters <em>macro name</em> in the code, it will replace it with the replacement text.</p>
<p>The preprocessor will <strong>not</strong> replace macro names inside of strings.</p>
<pre>#define my_macro "a replacement string"
// this will print <span class="i2">a replacement string</span> without quotes
printf(my_macro);
// this will print <span class="i2">my_macro</span> because my_macro is inside a string
printf("my_macro");</pre>
<h1>Macros with Parameters</h1>
<p>Macros can also take parameters. Text passed as parameters replaces the parameter placeholder in the replacement text.</p>
<div class="c1">
<p>When declaring a macro that takes parameters, it is important that the opening parenthesis <code>(</code> immediately follows the macro name. If there is a space between the macro name and <code>(</code> then everything following the macro name is considered to be the replacement text.</p>
<p>This is a macro <strong>without </strong>parameters:</p>
<pre>#define max (a, b) a > b ? a : b
<strong>           ^
The space marks the end of the macro name,
everything following is replacement text.</strong>
</pre>
</div>
<p><pre>#define square(a) a*a </pre>
</p>
<p>The macro name is <code>square</code>.</p>
<p>The macro parameter is <code>a</code>.</p>
<p>The replacement text is <code>a*a</code>.</p>
<p>Whatever text you pass in the <code>a</code> parameter replaces <code>a</code> in the replacement text.</p>
<p>After the parameters are replaced, that new replacement text replaces <code>square</code> in your code.</p>
<p>After preprocessing, the following source code:</p>
<pre>int number = 5;
int c = square(number);</pre>
<p>ends up looking like this (this is what the C/C++ compiler sees after preprocessing):</p>
<pre>int number = 5;
int c = number * number;</pre>
<p>which looks as we would expect it &#8211; all occurrences of <code>a</code> have been replaced by the parameter we passed (<code>number</code>).</p>
<p>The preprocessor only performs text replacement, so we can also write the following:</p>
<pre>int c = square(12 + 3);</pre>
<p>This gets preprocessed into:</p>
<pre>int c = 12 + 3 * 12 + 3;</pre>
<p>The compiler will parse it as follows (following operator precedence):</p>
<pre>int c = 12 + (3 * 12) + 3;</pre>
<p>which does not give us the answer we want. Remember, the preprocessor replaces the parameter with the text passed to it. In this case, each occurrence of <code>a</code> is replaced with <code>12 + 3</code>. This is an example of an &#8220;unintended side effect&#8221; using a macro. The way to fix this is to ensure each replacement of <code>a</code> is evaluated in its entirety. We do this by adding parentheses around the parameter in the replacement text:</p>
<pre>#define square(a) ((a) * (a)) </pre>
<p>Parentheses were also placed around the replacement text to ensure the entire replacement text is evaluated as a whole.</p>
<p>Even with parentheses, it is still possible to get unintended side effects:</p>
<pre>int number = 5;
int c = square(number++);</pre>
<p>When expanded, this becomes:</p>
<pre>int c = ((number++) * (number++));</pre>
<p>The side effect is that <code>number</code> is incremented twice and not once as would be assumed from reading <code>square(number++)</code>.</p>
<p>Pretty much any text can be passed as a macro parameter &#8211; even other macros. You will have difficulty passing a comma (<code>,</code>) since it delimits parameters and the closing parenthesis (<code>)</code>) since it marks the end of the macro parameter list. They can be passed without problem inside of a string.</p>
<h1>Macros as Parameters</h1>
<p>When a macro is passed as a parameter, the macro will be expanded before replacing the parameter in the replacement text. If the parameter is associated with the stringizing (<code>#</code>) or token pasting (<code>##</code>) operators, then the macro name will be used instead of being expanded.</p>
<pre>#define invalid_name "invalid name"
#define error "ERROR - "
#define error_msg(a) error a
.
.
.
printf(error_msg(invalid_name));</pre>
<p>In the <code>printf</code> statement, the macro <code>error_msg</code> is passed the macro <code>invalid_name</code> as its parameter. The preprocessor will replace the macro name <code>invalid_name</code> with its replacement text <code>"invalid name"</code>. It then processes the replacement text <code>error a</code> by (1) replacing the macro <code>error</code> with its replacement text, (2) replacing parameter <code>a</code> with the previously expanded <code>"invalid name"</code>. Once expanded, the compiler sees the following:</p>
<pre>printf("ERROR = ""invalid name");</pre>
<p>Since there are two juxtaposed strings, they will be concatenated:</p>
<pre>printf("ERROR = invalid name");</pre>
<p>Passing macros as macro parameters allows you to pass characters you normally wouldn&#8217;t be able to.</p>
<p><code>my_macro</code> defines a simple <code>printf</code> as replacement text. It is obvious that parameter <code>x</code> must be a comma (<code>,</code>) in order for the statement to compile. Trying to pass a comma in the parameter list will fail because it looks to the preprocessor that <code>my_macro</code> is being passed 3 parameters (when it was defined to accept only two):</p>
<pre>#define my_macro(x, y) printf("Number passed to macro was %d" x y)
.
.
.
my_macro(,,10);</pre>
<p>We can get around this by defining a macro for the comma:</p>
<pre>#define comma ,
#define my_macro(x, y) printf("Number passed to macro was %d" x y)
.
.
.
my_macro(comma, 10);</pre>
<h1>Variadic Macros</h1>
<p>In C99 and in the upcoming C++0x versions of the languages, macros can also be defined with a variable number of parameters:</p>
<pre>#define my_macro(...)
#define my_other_macro(a, b, ...)</pre>
<p>The ellipsis (<code>...</code>) specifies that a variable number of parameters are permitted in place of the ellipsis. The ellipsis must be the last parameter in the macro. (If it is the only parameter in the macro, then it counts as being the last parameter.)</p>
<p>In the replacement text, the parameters taking the place of the ellipsis &#8211; including the commas &#8211; are referenced using the identifier <code>__VA_ARGS__</code>. Before being assigned to <code>__VA_ARGS__</code>, the preprocessor performs all macro replacements on the arguments:</p>
<pre>#define g1 "gyre"
#define g2 "gimble"
#define my_macro(...) printf(__VA_ARGS__)
.
.
.
my_macro("the slithy toves did %s and %s", g1, g2);</pre>
<p><code>my_macro</code> was defined as taking a variable number of arguments. These arguments were then passed to <code>printf</code>. Before passing the variable arguments &#8211; referenced as <code>__VA_ARGS__</code> &#8211; to <code>printf</code>, the preprocessor replaces the macros <code>g1</code> and <code>g2</code> with their respective replacement texts. This results in the following expansion:</p>
<pre>printf("the slithy toves did %s and %s", "gyre", "gimble");</pre>
<div class="c1">
<p>Using <code>...</code> tells the preprocessor that everything following the preceding comma <code>,</code> up to the closing parenthesis <code>)</code> is just one argument called __VA_ARGS__. This is one way you can pass commas as arguments without using the <code>comma</code> macro trick described earlier. If you want to pass a closing parenthesis, you still have to use a macro.</p>
<pre>#define my_macro(a, b, ...)
<strong>                 ^  ^  ^
                 |  |  |
                 |  |  | everything until the ) replaces
                 |  |  |__VA_ARGS__ in the replacement
                 |  |  |text
                 |  | what is passed here replaces b in the
                 |  | replacement text
                 |what is passed here replaces a in the
                 |replacement text</strong></pre>
<p>the only difference between the parameter <code>...</code> and parameters <code>a</code> and <code>b</code> is that <code>...</code> can include commas. For <code>a</code> and <code>b</code> as soon as a comma is encountered, the preprocessor assumes it has reached the end of the parameter.</p>
<p>I don&#8217;t know why they didn&#8217;t just call <code>...</code> <code>__VA_ARGS__</code> instead:</p>
<pre>#define my_macro(a, b, __VA_ARGS__)</pre>
<p>because it would have been more obvious what was going on. The only thing special about <code>...</code> is that the preprocessor does not process the commas (<code>,</code>) for you &#8211; you are responsible for parsing the arguments out of __VA_ARGS__ (or just passing it to something that knows how to parse arguments).</p>
</div>
<h1>Macro re-Processing</h1>
<p>After the <code><a href="//complete-concrete-concise.com/programming/c/preprocessor-%e2%80%93-understanding-the-stringizing-operator">#</a></code> and <code><a href="//complete-concrete-concise.com/programming/c/preprocessor-the-token-pasting-operator">##</a></code> operations have been performed, the parameters expanded (if they were macros) and inserted into the replacement text, then the replacement text is processed again looking for macros to replace. However, if any macros are found that have already been replaced, then they are <strong>NOT</strong> replaced again.</p>
<pre>#define comma ,
#define reparse(x, y) x##y
.
.
.
printf("This is a %s" reparse(com, ma) "string");</pre>
<p>When <code>reparse(com, ma)</code> is processed, the result is the macro name <code>comma</code>, which is then replaced by the symbol <code>,</code></p>
<p>The following:</p>
<pre>#define comma reparse(com, ma)
#define reparse(x, y) x##y
reparse(com, ma);</pre>
<p>will result in <code>reparse(com, ma)</code> being processed into the macro <code>comma</code>. The macro <code>comma</code> will be re-evaluated and processed into <code>reparse(com, ma)</code>. At this point, the preprocessor will stop re-evaluating the macro because it already evaluated the macro <code>reparse</code> earlier in this cycle of evaluations.</p>
<h1>Macro Redefinition</h1>
<p>All macro names exist in the same namespace, so all macro names must be unique.</p>
<p>If the preprocessor encounters a duplicate macro name, it checks to see if the macro is identical to the original definition &#8211; if it is not, the program is considered malformed.</p>
<p>Leading and trailing whitespace are not considered significant. The following are considered identical:</p>
<pre>#define my_macro(a) a*a
#define my_macro( a ) a*a
#define my_macro(a) /* comments are */ a*a /* whitespace */</pre>
<p>Different parameter names are considered significant. The following are considered different and indicate a malformed program if they appear in the same program:</p>
<pre>#define my macro(a) a*a
#define my_macro(b) a*a
#define my_macro(b) b*b</pre>

<p>The post <a href="https://complete-concrete-concise.com/programming/c/preprocessor-the-define-directive/">Preprocessor &#8211; The #define Directive</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Compilation Error &#8211; Improperly Defined Macro</title>
		<link>https://complete-concrete-concise.com/programming/c/compilation-error-improperly-defined-macro/</link>
		
		<dc:creator><![CDATA[richardsplanet]]></dc:creator>
		<pubDate>Wed, 08 Jun 2011 04:57:19 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[compile error]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[macro]]></category>
		<guid isPermaLink="false">http://complete-concrete-concise.com/programming/c/compilation-error-improperly-defined-macro</guid>

					<description><![CDATA[<p>Macros are tricky things to use in C/C++ programming. When they work, they work great. When they have bugs, they are a pain to troubleshoot. Macros are a simple text substitution done by the preprocessor. Whenever the macro name is encountered in your code, the preprocessor replaces it with the text to the right of [&#8230;]</p>
<p>The post <a href="https://complete-concrete-concise.com/programming/c/compilation-error-improperly-defined-macro/">Compilation Error &#8211; Improperly Defined Macro</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Macros are tricky things to use in C/C++ programming. When they work, they work great. When they have bugs, they are a pain to troubleshoot.</p>
<p>Macros are a simple text substitution done by the preprocessor. Whenever the macro name is encountered in your code, the preprocessor replaces it with the text to the right of the macro name (plugging in any optional parameters you may have passed).</p>
<p>A macro is composed of three parts:</p>
<ol>
<li><strong>#define</strong> &#8211; this introduces the macro</li>
<li><strong>macro name</strong> and optional parameter list. The macro name cannot have any spaces and the parameter list cannot be separated from the macro name with any spaces.</li>
<li><strong>text that will replace the macro</strong> &#8211; everything following the macro name (and optional parameter list), until the end of the line, is what the preprocessor will use to replace the macro name. For long macros, or for better formatting, the macro can be defined over several lines by ending each line with a backslash character  (\) and a new line (enter) (extra spaces between the backslash character and new line means the macro ends there &#8211; so be careful of extra spaces following the backslash).</li>
</ol>
<h1>Working Examples:</h1>
<p><code></p>
<pre>#define my_macro // preprocessor will replace my_macro with this comment.</pre>
<p></code><br />
<code></p>
<pre>#define my_macro // when the preprocessor encounters my_macro, \
                    it will replace it with everything on this \
                    line that is to the right of the definition \
                    (which is this comment). Fortunately, \
                    it is possible to spread the line over several \
                    lines by using the backslash character</pre>
<p></code></p>
<h1>Not Working Examples:</h1>
<p>The following macro won&#8217;t compile following expansion (it is supposed to replace the macro with the contents of the parameter list):</p>
<div class="c2"><code>#define my_macro (x) x</code><br />
<code>my_macro(int a);</code></div>
<p>The (unhelpful but correct) error message from the compiler (GCC v.4.4.1) is:</p>
<div class="c4"><code>error: expected '=', ',', ';', 'asm' or '__attribute__' before 'x'</code></div>
<p>The problem is the space between the macro name and the parameter list &#8211; there should be none.</p>
<p>Here&#8217;s another macro that won&#8217;t compile (like the one above, it is supposed to replace the macro with the parameters passed) :</p>
<div class="c2"><code>#define my_macro (x, y) x y</code><br />
<code>my_macro(int, abc);</code></div>
<p>The (unhelpful but correct) from the compiler (GCC v.4.4.1) is:</p>
<div class="c4"><code>error: expected ')' before ',' token</code></div>
<p>Again, the problem is a space between the macro name and the parameter list. Removing the space fixes the problem.</p>
<div class="c1">
<p>It is an <strong>error</strong> to separate the macro name and the macro parameter list with a space.</p>
</div>

<p>The post <a href="https://complete-concrete-concise.com/programming/c/compilation-error-improperly-defined-macro/">Compilation Error &#8211; Improperly Defined Macro</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
