Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

7.1.4p2 vs. Annex J

34 views
Skip to first unread message

Keith Thompson

unread,
Aug 3, 2020, 11:42:53 PM8/3/20
to
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.

Is the intent behind that paragraph in J.2 something other than
what it seems to be? (Or am I misreading it?) Or is it just wrong?

Other than declaring a function, as permitted by 7.1.4p2, how would
a program even violate that paragraph in J.2 without also violating
a constraint?

--
Keith Thompson (The_Other_Keith) Keith.S.T...@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

Ike Naar

unread,
Aug 4, 2020, 1:26:53 AM8/4/20
to
On 2020-08-04, Keith Thompson <Keith.S.T...@gmail.com> wrote:
> 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.

The header that declares the puts() function is <stdio.h>.
The above program does not include <stdio.h>.
So the puts() function is not used before <stdio.h> is included.

Philipp Klaus Krause

unread,
Aug 4, 2020, 4:53:44 AM8/4/20
to
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.

Philipp

Keith Thompson

unread,
Aug 4, 2020, 10:29:32 AM8/4/20
to
Please quote (some of) the article you're replying to.

In N2479, J.2 still says 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).

The normative text is clear enough in both N1570 and N2479. The text in
J.2 is at best unclear. That's the error I'm referring to.

Andy Walker

unread,
Aug 5, 2020, 5:47:06 AM8/5/20
to
On 04/08/2020 04:42, Keith Thompson wrote:
> 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).

This is a fine example of the ambiguity in English that makes
writing standards therein fraught with difficulties. The structure is
"UB if A before B". Keith reads that as forbidding "A" without "B",
the same structure as "you can't go out before Saturday"; others read
it as "A" being allowed by itself or following "B", but not if there
is a later "B", the same structure as "you can't eat before swimming".
So it's ambiguous.

I used to use "while" to illustrate a similar ambiguity; the
UK used to have a road sign "wait here while lights are red" in front
of temporary signals for road works. But whereas most people read
"while" as meaning "for as long as" [also the programming usage],
in parts of the UK it means "until" ["wait here while the doctor
comes"], which left locals [allegedly] waiting on the green light
and driving through on red [even in Yorkshire, they're probably
not that daft, though it might be an ingenious excuse following an
accident].

This article is brought to you from the campaign for formal
definitions of languages. [There has been a relevant thread over
in "comp.lang.misc".]

--
Andy Walker,
Nottingham.
0 new messages