title: Print statically allocated array contents at compile time
body: <p>In C, is it possible to print (at compile time) the contents of a statically defined array if the inclusion of various array elements is determined by <code>#define</code> values?</p>
  <p>I have seen many questions about <code>#pragma message</code> (seems helpful) but none about how to do what I am trying to do (though I may be searching for the wrong thing).</p>
  <p>Here is what I am trying to accomplish:</p>
  <p>I am working with a large 3rd party open source library that (among other things) includes a large array of information for which the contents of the array never change and the length of the array is determined by whether or not (tons and tons of) preprocessor symbols are defined. I need to be able to print <strong>AT COMPILE TIME</strong> what the array contains.</p>
  <p>Here is a very small program to show the gist of what I am getting at:</p>
  <pre><code>#include &quot;stdlib.h&quot;
  #include &quot;stdio.h&quot;
  
  // there are tons of these, some of which are conditionally defined based on tons of other defines. 
  // All are defined in other header files and none are defined via the command line, i.e. via `-DTHREE`.
  #define ONE
  #define TWO
  // ...
  
  int main()
  {
    int values[] = {
  #ifdef ONE
    1,
  #endif
  #ifdef TWO
    2,
  #endif
  #ifdef THREE
    3,
  #endif
  #ifdef FOUR
    4,
  #endif
  #ifdef FIVE
    5,
  #endif
    0
    };
    int i = 0;
  
    for (i = 0; values[i] != 0; i++)
    {
      printf(&quot;values[%d] = %d\n&quot;, i, values[i]);
    }
    return 0;
  }
  </code></pre>
  <p>Obviously, if I compile this code (with <code>gcc</code>) and <strong>run</strong> it then I get the output:</p>
  <pre><code>values[0] = 1
  values[1] = 3
  </code></pre>
  <p>However, what I want to know is that the values 1 and 3 are present in the array <strong>at compile time</strong>. To be clear, I do not care if any particular value is or is not in the array; I only want to know what is actually in the array <strong>without having to run the binary</strong>. Ideally, my workflow would be to compile the code and search the output for specially formatted messages that would tell me what the array contains. The code should always compile as my intent is that the added diagnostics will tell me <em>about</em> the binary not prevent it from compiling if it does or does not include something.</p>
  <p>I have tried to do the following:</p>
  <ul>
  <li>Use <code>#pragma message</code>. So something like:</li>
  </ul>
  <pre><code>  // ...
  #ifdef ONE
  #pragma message &quot;1 is included&quot;
    1,
  #endif
    // ...
  </code></pre>
  <p>This causes compile errors (<code>error: expected expression before '#pragma'</code>)</p>
  <ul>
  <li>Use <code>#warning</code>. So something like:</li>
  </ul>
  <pre><code>  // ...
  #ifdef ONE
  #warning &quot;1 is included&quot;
    1,
  #endif
    // ...
  </code></pre>
  <p>This works very well <em>but</em> as soon as I use <code>-Werror</code> (which I need to do) compilation fails.</p>
  <p>Any thoughts on how to accomplish this would be great.</p>
  <p><strong>UPDATE/CLARIFICATION:</strong> in my simple example, the <code>#ifdef</code> guards around each element's inclusion are just checking a single symbol. This is not reflective of the actual code; the actual code's preprocessor guards check many, many, many symbols to determine if a single array element should be included or not. As such, it is not practical for me to check a list of symbols to determine what is in the list as I would actually have to duplicate the complex preprocessor symbol checks.</p>