I was given the code:
#include <stdio.h>
int main(void) {
int a = 0, b = 0, c = 0;
c = (a -= a - 5), (a = b, b + 3);
printf("a = %d, b = %d, c = %d", a, b, c); // a = 0, b = 0, c = 5
}
and my task was to figure out how it works. I do not understand how the line
c = (a -= a - 5), (a = b, b + 3);
works, though.
My guess is that it works just like this code:
#include <stdio.h>
int main(void) {
int a = 0, b = 0, c = 0;
a -= a - 5;
c = a;
a = b;
b + 3;
printf("a = %d, b = %d, c = %d", a, b, c); //a = 0, b = 0, c = 5
}
because the result is the same.
But I don't understand the first version of the code. So firstly, it changes the value of a (a becomes 5), then changes the value of c (c becomes a), and then it performs the expression in the second parentheses (which seems to not affect the value of c at all, although it's the same line of code and there was no semicolon in between, only a comma. what is that syntax? Is there a name for that kind of syntax? i just don't understand why it was written like that and why it does what it does.
Also, less importantly, what does the line:
b + 3;
do? Is it just nothing?
b + 3line is a no-op — most compilers will remove it (certainly if any optimization is used; probably even without optimization).