title: Do any C or C++ compilers optimize within define macros?
body: <p>Let's say I have the following in <strong>C</strong> or C++:</p>
  
  <pre><code>#include &lt;math.h&gt;
  #define ROWS 15
  #define COLS 16
  #define COEFF 0.15
  #define NODES (ROWS*COLS)
  #define A_CONSTANT (COEFF*(sqrt(NODES)))
  </code></pre>
  
  <p>Then, I go and use <code>NODES</code> and <code>A_CONSTANT</code> somewhere deep within many nested loops (i.e. used many times). Clearly, both have numeric values that can be ascertained at compile-time, but do compilers actually do it? At run-time, will the CPU have to evaluate <code>15*16</code> every time it sees <code>NODES</code>, or will the compiler statically put <code>240</code> there? Similarly, will the CPU have to evaluate a square root every time it sees <code>A_CONSTANT</code>?</p>
  
  <p>My guess is that the <code>ROWS*COLS</code> multiplication is optimized out but nothing else is. Integer multiplication is built into the language but sqrt is a library function. If this is indeed the case, is there any way to get a magic number equivalent to <code>A_CONSTANT</code> such that the square root is evaluated only once at run-time?</p>