That's a poor analogy. The behavior on division by zero is entirely
undefined. Whether a text stream requires a trailing new-line is
implementation-defined, and must be documented by each implementation.
Yes. So each implementation must have a document which says:
``... a trailing newline is required.''
or
``... a trailing newline is not required.''
This is not implementation-defined /behavior/, but an implementation-
defined presence or absence of a requirement---a rather curious
animal.
The requirement belongs to the C standard, but whether or not this
requirement is ``enabled'' is determined by the implementation.
It's as if the standard's text was configurable in the following
manner:
#ifdef CONFIG_TEXT_STREAM_REQUIRES_NEWLINE
The last line written to a text stream shall be terminated by a
newline.
#endif
where the implementation controls whether this part of the text is
enabled.
But of course, implementations don't actually control the set of
requirements in the standard. The only way the above situation can be
achieved is if the standard imposes the requirement, and the
implementation either preserves it as is, or waives it.
Looking at this another way, using the standard only, what can we say
about the behavior of a program which doesn't terminate a text stream
with a newline? We know that it violates a requirement in some
implementations but not in others. Well, that's as good as undefined
behavior. If the behavior is undefined in any possible implementation,
then it's undefined.
Effectively, the standard requires text streams to be newline
terminated, but requires implementations to either document that this
requirement is waived in the local dialect, or else to document that
the requirement is upheld. But implementations can lift just about any
requirement in this way, such as the requirement that the divisor
operand in a division be non-zero, or that a dereferenced pointer be
non-null, etc.
Let's reason a little bit about how useless this is. There are two
cases: either the implementation upholds the requirement or it
doesn't. In the first case, the piece of documentation which says
``the newline is required'' is completely redundant, so it is useless
to require it. We know that it's required from the standard already!
In the second case, the documentation says ``the newline is not
required'', which is---doh---a documented extension over undefined
behavior. But there is already a blanket requirement in the standard
that extensions be documented! So it's redundant to reiterate this
requirement. Useless, again!
So the bottom line is that a statement of the form ``it is
implementation-defined whether X is required'' is just confusing
verbiage which logically means the same thing ``X is required''.
A better analogy might be:
int main(void) { int i = 40000; return 0; }
This has well defined behavior for an implementation with
INT_MAX >= 40000, and undefined behavior for an implementation
with INT_MAX < 40000.
Not sure about that one. The worst thing that happens is that an
implementation-defined value is stored in i. Narrowing integer
conversions don't produce undefined behavior, but an implementation-
defined result. (Of course, that result can be embroiled in some
computation which can produce undefined behavior due to that result).
But how about this:
#include <limits.h>
int main(void)
{
if (INT_MAX < 40000)
*((char *) 0) = 0;
return 0;
}
This clearly has undefined behavior according to standard C, but in
specific dialects, it has a defined meaning. Those dialects would be
ones where INT_MAX >= 40000, or ones in which storing a byte through
the null pointer is a harmless operation (and documented as such).
I wouldn't call such implementations "dialects";
they're merely common variations of C.
Every implementation accepts some kind of dialect. When we write a
strictly conforming ISO C, we are also writing in a special dialect.
Programs can be defined or undefined in implementation-specific
dialects (using their notion of ``defined'' or ``undefined'', of
course).
Well-defined standard C programs are well-defined in every conforming
dialect.