Re: C Test Incorrectly Uses printf() - Please Confirm



Tom St Denis <tom@xxxxxxx> writes:
[...]
All I read that as is the value of 'a + 5' is not defined. All the
work up to that point IS defined. computing an expression like 'a +
5' can have no side effects in this application since it's either 6 or
7 which are both valid 'int' values [no overflow].

I get what you're saying that since the ENTIRE statement is not
composed of defined expressions it's rejected as UB, but if we speak
practically for a second, it's a throwaway statement that can't have
side effects [other than taking time to compute].

In fact, since it's not even printed out it could just optimize out
the expression altogether.

I would therefore expect with every C compiler on this planet in
common use that the statement would print '2' as its output with
absolutely no undefined behaviour whatsoever. More so, I think this
is just a whole in the spec in which you have UB but it could be
defined [or at least mitigated] with a bit of logical deduction.

The real point of undefined behavior is not that any real-world
implementation is likely to behave in any particular way. The point --
what "undefined behavior" *means* -- is that, if a program executing the
statement

printf("%d", ++a, a + 5);

prints "kablooie" or emits a suffusion of yellow, that doesn't
imply that the implementation is non-conforming. The Standard
"imposes no requirements"; that's all it means.

[...]

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.