title: Macro and Pre/Post Increment in C
body: <p>I have noticed this strange behavior of macro functions while using pre increment operator. I know it is not preferable to use pre increment operator with macros but still I would like to know the reason behind the execution of the following 2 codes:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #define SQR(x) {x*x }
  
  int main()
  {
      int a=4;
      int b=SQR(a++);
      int c=SQR(++a);
      printf("%d.....%d....%d\n",b,c,a*b*c); 
      return 0;
  }
  </code></pre>
  
  <p>The output of this is:</p>
  
  <pre><code>20.....64....10240
  </code></pre>
  
  <p>The first value b is 20, which is okay 4*5. But why the value of C is 64 i.e. 8*8 instead of 7*8?</p>
  
  <p>And I just replaced the sequence of execution like this:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #define SQR(x) {x*x }
  
  int main()
  {
      int a=4;
      int c=SQR(++a);
      int b=SQR(a++);
      printf("%d.....%d....%d\n",b,c,a*b*c); 
      return 0;
  }
  </code></pre>
  
  <p>The output of this is:</p>
  
  <pre><code>42.....36....12096
  </code></pre>
  
  <p>Strange isn't it? The pre increment again had some issue. It gave 42 i.e. 6*7 instead of 5*6 and after that the post increment also gave wrong answer 36 i.e. 6*6 instead of 6*7.</p>
  
  <p>It would be a great help if anyone can explain why the outputs are like this?</p>