Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Let's take it step by step</p> <pre><code>#define MAX 10 </code></pre> <p>This will replace every instance of the word "MAX" by 10 in your code file. this is very much like defining a constant variable with one major difference. The interpretation of #define statement is done way before compilation. which helps for an example to use MAX as an array size. Which would have caused a compiler error in many cases if you have used a variable instead.</p> <p>you can use <code>cpp &lt;filename.c&gt;</code> command in Linux terminal to see what will happen when a macro is executed.</p> <p>for an example this code:</p> <pre><code>#define MAX 10 int numbers[MAX]; </code></pre> <p>after preprocessing (i.e. interpretation of the preprocessor macros)</p> <pre><code>int numbers[10]; </code></pre> <p>note that the #define statement will vanish once interpreted.</p> <p>This takes us to another example</p> <pre><code>#define square(x) (x * x) </code></pre> <p>every instance of <code>square(x)</code> in our code will not only be replaced by <code>(x * x)</code> but also the value of x will be replaced. Which has an effect similar to function deceleration but again it is different</p> <p>so </p> <p><code>square(5)</code> will be replaced by <code>(5 * 5)</code></p> <p>Finally our example</p> <pre><code>#define, EQ(a, b) ((a) == (b)) </code></pre> <p>This will replace every instance of <code>EQ(a, b)</code> by <code>((a) == (b))</code></p> <p>so for an example </p> <p><code>EQ(4, 5)</code> will be replaced by <code>((4) == (5))</code></p> <p>Now what does "==" mean? it is the "check if equal" if 4 and 5 are equal the whole expression would evaluate as 1 (which obviously is not true) and thus this expression will end up to be evaluated as 0.</p> <p>Which more or less like <strong>the effect of</strong> this function</p> <pre><code>int EQ(int a, int b) { return (a == b); } </code></pre> <p>Which could be also written as</p> <pre><code>int EQ(int a, int b) { if (a ==b) return 1; if (a !=b) return 0; } </code></pre> <p>Note that with the macro we avoided two variable declarations and there is no function call really and it is in general quicker. This advantage will be obvious if you have a slower processor (or microprocessor).</p> <p>Finally let me state the obvious</p> <pre><code>#define simple_macro 5 int some_integer_variable = 10; . . . some_integer_variable = simple_macro; simple_macro = 12; /* illegal statement */ simple_macro++; /* illegal statement */ </code></pre> <p>because after running the preprocessor this will be</p> <pre><code>int some_integer_variable = 10; . . . some_integer_variable = 5; 5 = 12; 5 ++; </code></pre> <p>Oh! I might have talked too much but let me leave you with this (obvious) code</p> <pre><code>#define MAX 10 int variable = MAX; /*this is a MAX variable */ char some_string[] = "the MAX value"; /* no replacement will happen here */ int variable_MAX; /* no replacement will happen here */ </code></pre> <p>after running the preprocessor will be</p> <pre><code>int variable = 10; char some_string[] = "the MAX value"; int variable_MAX; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload