Discussion:
Are important details of the standard specified inside round brackets or am I missing something?
(too old to reply)
James R. Kuyper
2016-06-20 17:41:11 UTC
Permalink
Raw Message
Does identifier of function parameter within function definition have function prototype scope?
"If the declarator or type specifier that declares the identifier
appears within the list of parameter declarations in a function
prototype (not part of a function definition), the identifier has
function prototype scope, which terminates at the end of the function
declarator."
And I'm asking is the text in round brackets only for the reader?
Maybe not as function prototype is defined in similar context (6.2.1
... "There are four kinds of scopes: function, file, block, and
function prototype. (A function prototype is a declaration of a function
that declares the types of its parameters.)"
Or maybe I'm wrong and the meaning of function prototype is defined
somewhere else ...
The second occurrence of "function prototype" in 6.2.1p2 is italicized.
That is an ISO convention indicating that sentence containing that
phrase is the official definition of that term - there is no other
definition.
... in a way that wouldn't allow it to be part of a function
definition. ...
It's not that a function prototype is not allowed to be part of a
function definition - in fact, in modern C all function definitions
should start with a function prototype, and most do. The key point is
that a function prototype IS allowed to appear outside of the function
definition.

void func(int declaration);

void func(int definition) { printf("%d\n", definition); }

The first prototype for func() above is a function declaration, and is
"not part of a function definition", as specified in 6.2.1p4. As a
result, "declaration" has function prototype scope.

However, in the same clause (6.2.1p4), the sentence that immediately
precedes the one that you quoted says:

"If the declarator or type specifier that declares the identifier
appears inside a block or within the list of parameter declarations in
a function definition, the identifier has block scope, which terminates
at the end of the associated block." (6.2.1p4)

The second prototype for func() above does occur "in a function
definition", so "definition" has block scope.
... And so the above 2 texts in round brackets will be left
optional and primary for clarification to the reader.
No, they are not just for clarification. What's inside those parentheses
serves the very important role of distinguishing the rule that applies
to a function prototype that occurs "in a function definition" from the
rule that applies for a function prototype that is "not part of a
function definition".
Kaz Kylheku
2016-06-20 20:55:19 UTC
Permalink
Raw Message
Post by James R. Kuyper
Does identifier of function parameter within function definition have function prototype scope?
"If the declarator or type specifier that declares the identifier
appears within the list of parameter declarations in a function
prototype (not part of a function definition), the identifier has
function prototype scope, which terminates at the end of the function
declarator."
And I'm asking is the text in round brackets only for the reader?
Maybe not as function prototype is defined in similar context (6.2.1
... "There are four kinds of scopes: function, file, block, and
function prototype. (A function prototype is a declaration of a function
that declares the types of its parameters.)"
Or maybe I'm wrong and the meaning of function prototype is defined
somewhere else ...
The second occurrence of "function prototype" in 6.2.1p2 is italicized.
That is an ISO convention indicating that sentence containing that
phrase is the official definition of that term - there is no other
definition.
Ergo, even though it is written that a definition with a
parameter-type-list "serves as a" prototype, that doesn't make it one.
I don't follow that. Because it serves as a prototype, it IS a
declaration of the function, and it DOES declare the types of it's
parameters - so I don't see how it fails to qualify as a prototype. What
part of the definition do you think it fails to meet?
Ah ok; in that case the parenthesized text is needed (the exclusion
of a function definition from having function prototype scope).

I thought you may have found a way that it follows from the definition
of "function prototype" alone, whether or not the parenthesized remark
is there.
Tim Rentsch
2016-07-11 17:39:46 UTC
Permalink
Raw Message
Does identifier of function parameter within function definition have
function prototype scope?
"If the declarator or type specifier that declares the identifier
appears within the list of parameter declarations in a function
prototype (not part of a function definition), the identifier has
function prototype scope, which terminates at the end of the
function declarator."
And I'm asking is the text in round brackets only for the reader?
[...]
The short answer is that text inside parentheses (aka round
brackets) is normative, and has just the same weight as text
outside parentheses.

A longer answer is that the authors are showing an awareness of
different ways that some key terms are used. In conventional
parlance, /declarations/ and /definitions/ are disjoint: part of
a program can be one or the other, but not both. As C is defined
in the ISO Standard, a /definition/ is always also a /declaration/,
which is different from how the terms are commonly used. The
text is this section is written to emphasize the distinction, and
so to avoid confusion about how the terms are being used. It may
help to consider the quoted sentence in conjunction with the
previous one:

[...] If the declarator or type specifier that declares the
identifier appears inside a block or within the list of
parameter declarations in a function definition, the
identifier has block scope, which terminates at the end of
the associated block. If the declarator or type specifier
that declares the identifier appears within the list of
parameter declarations in a function prototype (not part of
a function definition), the identifier has function
prototype scope, which terminates at the end of the function
declarator. [...]

The text as written shows clearly what is meant in the two cases,
without having to go rummaging around elsewhere in the Standard
to see how the various terms are defined.

Loading...