3 ms·
I've tried catching this little piece of undefined behaviour, but I was not able to (gcc.4.9 -fsanitize=undefined) #include <stdio.h> int main() { retu
by malkia 12y ago
I've tried catching this little piece of undefined behaviour, but I was not able to (gcc.4.9 -fsanitize=undefined)
#include <stdio.h>
int main() {
return printf("a") + (printf("b") + printf("c"));
}
- readerrrr 12y agoThat behavior is unspecified. The result might vary through compilers, but there is no undefined behavior. If your program can handle the functions being called in a different order you are safe, which is different from u.b. where the compiler can ( theoretically )do anything.
- cygx 12y agoWhat's undefined about that? The order of evaluation is only unspecified, ie you can't know which permutation of a,b,c you'll get - but you will end up with one of them (as long as IO doesn't fail)...
- malkia 12y agoThanks for cygx and readerrrr for correcting me. The difference (to me) of undefined and unspecified is very subtle and often escapes my interpretation when I have to use it later.
- taspeotis 12y agoIt's been a long time since I've done C/C++ programming. Is the behaviour undefined because of an overflow (number of characters written + number of characters written + number of characters written might > INT_MAX)? In the general case, that is. I imagine your program (as written) won't see an integer larger than 3.
- readerrrr 12y agoThe order of the printf() calls is in question. The order of evaluation is unspecified but not undefined.