I'm observing different behavior regarding what I think is part of the C standard between clang
and gcc
(either the homebrew version on my mac or on linux). The question is whether the comma in an argument list is a sequence point or not. clang
interprets it as such but gcc
does not.
This code demonstrates the issue:
#include <stdio.h>
int inner(int *v) {
*v += 1;
return 1;
}
int outer(int x, int y) {
return y;
}
int main(int argc, char *argv[]) {
int x = 4;
printf ("result=%d\n", outer(inner(&x), x));
}
And the results:
$ clang -o comseq comseq.c && ./comseq
result=5
$ gcc-4.8 -o comseq comseq.c && ./comseq
result=4
$ gcc-5 -o comseq comseq.c && ./comseq
result=4
I don't have access to a copy of the C standard at the moment, but I was pretty sure that clang
's behavior was correct until I saw gcc
's interpretation. Playing with the --std=
option didn't change my results at all.
ETA
On a better reading it is true that this question is answered in https://stackoverflow.com/a/4176333/3171657:
a , b (§5.18) (in func(a,a++) , is not a comma operator, it's merely a separator between the arguments a and a++. The behaviour is undefined in that case if a is considered to be a primitive type)
but that, while very useful, is a long answer so maybe leaving this question around will help other users like myself. Also, to be pedantic, that question is about C++, not C.