title: How to check if a parameter is an integral constant expression in a C preprocessor macro?
body: <p>I'm currently cleaning up an existing C-library to publish it shamelessly.</p>
  
  <p>A preprocessor macro <code>NPOT</code> is used to calculate the next greater power of two for a given integral constant expression <em>at compile time</em>. The macro is normally used in direct initialisations. For all other cases (e.g. using variable parameters), there is an inline function with the same function. </p>
  
  <p>But if the user passes a variable, the algorithm expands to a huge piece of machine code. My question is:
  <strong>What may I do to prevent a user from passing anything but an integral constant expression to my macro?</strong></p>
  
  <pre><code>#define NPOT(x)   complex_algorithm(x)
  
  const int c=10;
  int main(void) {
      int i=5;
  
      foo = NPOT(5);   // works, and does everything it should
      foo = NPOT(c);   // works also, but blows up the code extremely
      foo = NPOT(i);   // blows up the code also
  }
  </code></pre>
  
  <p>What I already tried:</p>
  
  <ol>
  <li>Define the macro to <code>#define NPOT(x)   complex_algorithm(x ## u)</code>. It still works and throws a - even if hardly helpful - compiler error for variable parameters. Unless there is no variable like iu... Dirty, dangerous, don't want it.</li>
  <li>Documentation, didn't work for most users.</li>
  </ol>