title: Use of comma separated variables in foor loop [C]
body: <p>In the linux kernel (ASOP) code I came across the following macro code:</p>
  <pre><code>#define for_each_cpu_and(cpu, mask, and)    \
      for ((cpu) = 0; (cpu) &lt; 1; (cpu)++, (void)mask, (void)and)
  </code></pre>
  <p>I have never come across a for loop like the one above where there are several comma separated variables along with the increment part. Using the following code I tried to check how the aforementioned macro actually behaves:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdbool.h&gt;
  
  #define for_each_cpu_and(cpu, mask, and)    \
      for ((cpu) = 0; (cpu) &lt; 1; (cpu)++, (void)mask, (void)and)
  
  int main()
  {
      int i;
      for_each_cpu_and(i, false, 3){
        printf(&quot;i value: %d\n&quot;, i);
      }
      return 0;
  }
  </code></pre>
  <p>The answer in the console is as follows:</p>
  <blockquote>
  <p>i value: 0</p>
  </blockquote>
  <p>If I tweek the code to the following:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdbool.h&gt;
  
  #define for_each_cpu_and(cpu, mask, and)    \
      for ((cpu) = 0; (cpu) &lt; 3; (cpu)++, (void)mask, (cpu)+2)
  
  int main()
  {
      int i;
      for_each_cpu_and(i, 4, 3){
        printf(&quot;i value: %d\n&quot;, i);
      }
      return 0;
  }
  </code></pre>
  <p>The answer in the console is as follows:</p>
  <blockquote>
  <p>i value: 0</p>
  <p>i value: 1</p>
  <p>i value: 2</p>
  </blockquote>
  <p>So from the aformentioned code it seems like in the increment part only the first increment option i.e. (cpu)++ is given precedence and others are not being used.</p>
  <p>Can someone please explain with example(s) the usage of additional comma separated variables in the increment part of the for loop?</p>
  <p><strong>Note</strong>: I am aware what a comma separated variable in C does in general and based on the rule the first varaible in the code should be given precedence. However, in the aforementioned code the case is not true. So explanation on the working of the comma separated variables in the increment part of the for-loop in the aforementioned code would be much appreciated.</p>
  <p>Comma separated variables in C: <a href="https://stackoverflow.com/questions/54142/how-does-the-comma-operator-work">How does the Comma Operator work</a></p>