title: Macro Expansion in C/C++ with __VA_ARGS__
body: <p>I found these two pieces of code related to what is a varidic macro</p>
  <p>1.The first example is used to achieve portability between the GNU C compiler and the Visual C Compiler, but I can't understand how this macro expands step by step</p>
  <pre><code> MACRO_VA_ARGS(foo, bar, baz)
  </code></pre>
  <p>Is the inner or outer parenthesis evaluated first?</p>
  <pre><code>#define MACRO_WITH_3_PARAMS(p1, p2, p3) P1 = p1 | P2 = p2 | P3 = p3
  #define PASS_ON(...) __VA_ARGS__
  
  #define MACRO_VA_ARGS(...) PASS_ON(PASS_ON(MACRO_WITH_3_PARAMS)( __VA_ARGS__))
  MACRO_VA_ARGS(foo, bar, baz)
  </code></pre>
  <p>All code for first example:</p>
  <pre><code>#define F_OR(i, a, b, s) for (int i=(a); (s)&gt;0?i&lt;(b):i&gt;(b); i+=(s))
  #define F_OR1(e) F_OR(i, 0, e, 1)
  #define F_OR2(i, e) F_OR(i, 0, e, 1)
  #define F_OR3(i, b, e) F_OR(i, b, e, 1)
  #define F_OR4(i, b, e, s) F_OR(i, b, e, s)
  #define GET5(a, b, c, d, e, ...) e
  #define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
  #define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
  </code></pre>
  <p>2.The second code example I found on Github, I know that</p>
  <pre><code>#define GET5(a, b, c, d, e, ...) e
  </code></pre>
  <p>extracts 5th element, but I don't understand how these two macros expand:</p>
  <pre><code>#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
  #define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
  </code></pre>
  <p>All code for second example:</p>
  <pre><code> #define F_OR(i, a, b, s) for (int i=(a); (s)&gt;0?i&lt;(b):i&gt;(b); i+=(s))
   #define F_OR1(e) F_OR(i, 0, e, 1)
   #define F_OR2(i, e) F_OR(i, 0, e, 1)
   #define F_OR3(i, b, e) F_OR(i, b, e, 1)
   #define F_OR4(i, b, e, s) F_OR(i, b, e, s)
   #define GET5(a, b, c, d, e, ...) e
   #define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
   #define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
  </code></pre>
  <p>Thank you!</p>