title: C macro stringifying a variable
body: <p>When can I pass variable's value to a macro for stringifying?</p>
  
  <p>For example the code taken from <a href="https://stackoverflow.com/a/3301451/1714692">this post</a> works with a constant-defined macro.</p>
  
  <pre><code>#define MAX_STRING_LENGTH 20
  #define STRINGIFY(x) STRINGIFY2(x)
  #define STRINGIFY2(x) #x
  
  {
    ...
    char word[MAX_STRING_LENGTH+1];     
    scanf("%" STRINGIFY(MAX_STRING_LENGTH) "s", word);
    ...
  }
  </code></pre>
  
  <p>However I cannot use it with a variable such as:</p>
  
  <pre><code>{
    ...
    int val = 20;
    char word[MAX_STRING_LENGTH+1];     
    scanf("%" STRINGIFY(val) "s", word);
    ...
  }
  </code></pre>
  
  <p>since the compilation is successful with this warning:</p>
  
  <pre><code>warning: invalid conversion specifier 'v' [-Wformat-invalid-specifier]
      scanf("%" STRINGIFY(var) "s", word);
             ~~~^~~~~~~~~~~~~
  test2.c:4:22: note: expanded from macro 'STRINGIFY'
  #define STRINGIFY(x) STRINGIFY2(x)
                       ^
  test2.c:5:23: note: expanded from macro 'STRINGIFY2'
  #define STRINGIFY2(x) #x
                        ^
  &lt;scratch space&gt;:466:2: note: expanded from here
  "var"
   ^
  1 warning generated
  </code></pre>
  
  <p>but the run of the code does not wait for any input.</p>
  
  <p>On the contrary in <a href="https://stackoverflow.com/q/14351971/1714692">this other post</a> it was possible to pass a variable to this macro:</p>
  
  <pre><code>#define PRINT(int) printf(#int "%d\n",int)
  ...
  int var =8;
  PRINT(var);
  </code></pre>
  
  <p>What is the difference between the two cases? How can I modify the first one so that it accepts also variables?</p>
  
  <p>I tried using <code>%d</code> inside the macro but I was not successful.</p>