Sujet : Re: question about linker
De : tr.17687 (at) *nospam* z991.linuxsc.com (Tim Rentsch)
Groupes : comp.lang.cDate : 11. Dec 2024, 17:52:54
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <86cyhyi2l5.fsf@linuxsc.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
User-Agent : Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Ben Bacarisse <
ben@bsb.me.uk> writes:
Bart <bc@freeuk.com> writes:
>
On 07/12/2024 21:00, David Brown wrote:
>
<snip purely subjective opinion on unrealistic code>
>
You mean that /real/ example where a function needed a type written 6 times
instead of once? OK.
>
I've always wondered why prototypes in C did not simply use the existing
syntax for declarations. After all, it was right there in K&R C, just
outside the parentheses:
>
f(m, n, s)
int m, n;
char *s;
{ ... }
>
could have become
>
f(int m, n; char *s)
{ ... }
>
rather than
>
f(int m, int n, char *s)
{ ... }
>
Does anyone know if there even /was/ a reason?
My speculation follows.
Prototypes in C came from C++. The language that became C++
started pretty early, before 1980 IIRC. To add types to function
parameters it would have been natural to simply add a type before
the named parameter. Of course to be more flexible a full
declarator rather than just a type name should be allowed. That
change should be easy to make in a C grammar and parser. The idea
of using multi-declarator declarations might not have occurred to
the pre-C++ language designer(s); or it may have been judged to
need too much effort or not be necessary (or add enough value).
Also there is another difference between declarations given in a
block/file scope and declarations given in a prototype scope: in
one case the order of declarators doesn't matter but in the other
case it does. Furthermore there is the question of what to do for
function declarations, as opposed to definitions, where parameter
names are not given, but just types.
Assuming all that was done before the C standardization effort
really got rolling, which I think is a distinct possibility, the
most likely decision would be simply to adopt the existing C++
syntax wholesale, and not consider changing it. Anyway that is my
guess.
If the question were being considered anew, I would be inclined to
come down on the side of keeping the one-parameter-only form, and
not allowing multiple parameters to share a single type name.
There doesn't seem to be much benefit in allowing more than one
declarator per type name; I think the strongest argument is
syntactic consistency with block/file/struct scope declarations,
but those contexts aren't quite the same as prototype scope (for
different reasons in the different cases). If there is no
significant benefit, simpler is better, and consistency be damned.
One change I would like to see made, and to have been made, is to
allow a trailing comma in function declarations, definitions, and
calls. A smaller change, and a larger benefit, would give a win
here. At least that is my thinking on the matter.