Joe Zbiciak and I discovered this in a discussion on Quora.
N1570 7.1.4p2 says:
Provided that a library function can be declared without reference
to any type defined in a header, it is also permissible to declare
the function and use it without including its associated header.
which means that this is a valid "Hello, world" program:
int puts(const char *s);
int main(void) {
puts("Hello, world");
}
But J.2 (which is non-normative) says that the behavior is undefined if:
A function, object, type, or macro that is specified as being
declared or defined by some standard header is used before any
header that declares or defines it is included (7.1.2).
which the above program violates.
J.2 is informative, and the part you quoted refers to normative text
7.1.2. In the current C2X draft N2479, the relevant sentenceg in 7.1.2 is:
"If used, a header shall be included outside of any external declaration
or definition, and it shall first be included before the first reference
to any of the functions or objects it declares, or to any of the types
or macros it defines."
So clearly, the undefined behaviour does not happen when the header is
not used at all.
But J.2 (which is non-normative) says that the behavior is undefined if:
A function, object, type, or macro that is specified as being
declared or defined by some standard header is used before any
header that declares or defines it is included (7.1.2).
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 379 |
Nodes: | 16 (0 / 16) |
Uptime: | 85:04:52 |
Calls: | 8,091 |
Files: | 13,069 |
Messages: | 5,849,016 |