Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>#define</code> is used to create macros in C and in C++. You can read more about it in the <a href="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/cpp_1.html#SEC10" rel="noreferrer">C preprocessor documentation</a>. The quick answer is that it does a few things:</p> <ol> <li><p>Simple Macros - basically just text replacement. Compile time constants are a good example:</p> <pre><code>#define SOME_CONSTANT 12 </code></pre> <p>simply replaces the text <code>SOME_CONSTANT</code> with <code>12</code> wherever it appears in your code. This sort of macro is often used to provide conditional compilation of code blocks. For example, there might be a header included by each source file in a project with a list of options for the project:</p> <pre><code>#define OPTION_1 #define OPTION_2 #undef OPTION_3 </code></pre> <p>And then code blocks in the project would be wrapped with matching <code>#ifdef</code>/<code>#endif#</code> blocks to enable and disable those options in the finished project. Using the <code>-D</code> gcc flag would provide similar behaviour. There are strong opinions as to whether or not this method is really a good way to provide configuration for an application, however.</p></li> <li><p>Macros with arguments - allows you to make 'function-like' macros that can take arguments and manipulate them. For example:</p> <pre><code>#define SQUARE(x) ((x) * (x)) </code></pre> <p>would return the square of the argument as its result; be careful about potential order-of-operations or side-effect problems! The following example:</p> <pre><code>int x = SQUARE(3); // becomes int x = ((3) * (3)); </code></pre> <p>will works fine, but something like:</p> <pre><code>int y = SQUARE(f()); // becomes int y = ((f()) * (f())); </code></pre> <p>will call <code>f()</code> twice, or even worse:</p> <pre><code>int z = SQUARE(x++); // becomes int z = ((x++) * (x++)); </code></pre> <p>results in undefined behaviour!</p> <p>With some tools, macros with arguments can also be <a href="http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html" rel="noreferrer">variadic</a>, which can come in handy.</p></li> </ol> <p>As mentioned below in the comments, overuse of macros, or the development of overly complicated or confusing macros is considered bad style by many - as always, put the readability, maintainability, and debuggability of your code above 'clever' technical tricks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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