A
arnuld
OBJECTIVE: To know why its UB
#include <stdio.h>
int main(void)
{
char c='8';
int d=8;
printf("%d %d %d\n",d, d+=c>='0'&& c<='9', c++);
return 0;
}
================ OUTPUT ===================
[arnuld@dune C]$ gcc -ansi -pedantic -Wall -Wextra test.c
test.c: In function ‘main’:
test.c:7: warning: operation on ‘d’ may be undefined
test.c:7: warning: operation on ‘c’ may be undefined
[arnuld@dune C]$ ./a.out
9 9 56
[arnuld@dune C]$
gcc says its UB. The way I understand it is:
(1) d is incremented in 2nd argument without defining any sequence
points, hence you can not guarantee what printf() will print as value of
d.
(2) same for c, as its being incremented without defining any sequence
point.
But as per C-FAQs here: http://c-faq.com/expr/seqpoints.html
comma operator defined a sequence point and I see comma is present
between printf() arguments but I am not sure if this "comma" is called as
"comma operator" ?
#include <stdio.h>
int main(void)
{
char c='8';
int d=8;
printf("%d %d %d\n",d, d+=c>='0'&& c<='9', c++);
return 0;
}
================ OUTPUT ===================
[arnuld@dune C]$ gcc -ansi -pedantic -Wall -Wextra test.c
test.c: In function ‘main’:
test.c:7: warning: operation on ‘d’ may be undefined
test.c:7: warning: operation on ‘c’ may be undefined
[arnuld@dune C]$ ./a.out
9 9 56
[arnuld@dune C]$
gcc says its UB. The way I understand it is:
(1) d is incremented in 2nd argument without defining any sequence
points, hence you can not guarantee what printf() will print as value of
d.
(2) same for c, as its being incremented without defining any sequence
point.
But as per C-FAQs here: http://c-faq.com/expr/seqpoints.html
comma operator defined a sequence point and I see comma is present
between printf() arguments but I am not sure if this "comma" is called as
"comma operator" ?