Hi all,
Why do I get a warning from gcc with the following program?
[test]$ cat test.c
#include <stdio.h>
int f(int n)
{
return n;
}
int main(void)
{
int n = 0;
printf("%i\n", f(n++) + n);
return 0;
}
[test]$ gcc -Wall test.c
test.c: In function ‘main’:
test.c:13: warning: operation on ‘n’ may be undefined
One of the sequence point cases is defined as: "The point of calling a
function, after evaluating its arguments." If I got it right, that rule
can be applied to `f(n++)' which is evaluated before `n' since addition
is left associative. I can't see the ambiguity here.
August
(I would never write this kind of code in practice, but it's good to
know how the standard is defined.)
Why do I get a warning from gcc with the following program?
[test]$ cat test.c
#include <stdio.h>
int f(int n)
{
return n;
}
int main(void)
{
int n = 0;
printf("%i\n", f(n++) + n);
return 0;
}
[test]$ gcc -Wall test.c
test.c: In function ‘main’:
test.c:13: warning: operation on ‘n’ may be undefined
One of the sequence point cases is defined as: "The point of calling a
function, after evaluating its arguments." If I got it right, that rule
can be applied to `f(n++)' which is evaluated before `n' since addition
is left associative. I can't see the ambiguity here.
August
(I would never write this kind of code in practice, but it's good to
know how the standard is defined.)
Comment