title: Is it possible to convert a C string literal to uppercase using the preprocessor (macros)?
body: <p>Ignoring that there are sometimes better non-macro ways to do this (I have good reasons, sadly), I need to write a big bunch of generic code using macros. Essentially a macro library that will generate a large number of functions for some pre-specified types.</p>
  
  <p>To avoid breaking a large number of pre-existing unit tests, one of the things the library must do is, for every type, generate the name of that type in all caps for printing. E.g. a type "flag" must be printed as "FLAG". </p>
  
  <p>I could just manually write out constants for each type, e.g.</p>
  
  <p>#define flag_ALLCAPSNAME FLAG</p>
  
  <p>but this is not ideal. I'd like to be able to do this programatically. </p>
  
  <p>At present, I've hacked this together:</p>
  
  <pre><code>char capname_buf[BUFSIZ];
  #define __MACRO_TO_UPPERCASE(arg) strcpy(capname_buf, arg); \
   for(char *c=capname_buf;*c;c++)*c = (*c &gt;= 'a' &amp;&amp; *c &lt;= 'z')? *c - 'a' + 'A': *c;
  __MACRO_TO_UPPERCASE(#flag)
  </code></pre>
  
  <p>which does what I want to some extent (i.e. after this bit of code, capname_buf has "FLAG" as its contents), but I would prefer a solution that would allow me to define a string literal using macros instead, avoiding the need for this silly buffer.  </p>
  
  <p>I can't see how to do this, but perhaps I'm missing something obvious?</p>
  
  <p>I have a variadic foreach loop macro written (like <a href="https://stackoverflow.com/a/1872506/747916">this one</a>), but I can't mutate the contents of the string literal produced by #flag, and in any case, my loop macro would need a list of character pointers to iterate over (i.e. it iterates over lists, not over indices or the like). </p>
  
  <p>Thoughts?</p>