In the linux kernel (ASOP) code I came across the following macro code:
#define for_each_cpu_and(cpu, mask, and) \
for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)and)
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:
#include <stdio.h>
#include <stdbool.h>
#define for_each_cpu_and(cpu, mask, and) \
for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)and)
int main()
{
int i;
for_each_cpu_and(i, false, 3){
printf("i value: %d\n", i);
}
return 0;
}
The answer in the console is as follows:
i value: 0
If I tweek the code to the following:
#include <stdio.h>
#include <stdbool.h>
#define for_each_cpu_and(cpu, mask, and) \
for ((cpu) = 0; (cpu) < 3; (cpu)++, (void)mask, (cpu)+2)
int main()
{
int i;
for_each_cpu_and(i, 4, 3){
printf("i value: %d\n", i);
}
return 0;
}
The answer in the console is as follows:
i value: 0
i value: 1
i value: 2
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.
Can someone please explain with example(s) the usage of additional comma separated variables in the increment part of the for loop?
Note: 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.
Comma separated variables in C: How does the Comma Operator work