pre-ansi declarations

J

j0mbolar

Which sections in the standard mandate that 'foo' below does
not qualify as a function that includes a prototype:

void foo(a)
char *a;
{
do_something(a);
}

int main(void)
{
foo();
return 0;
}



also, under 6.11.2 of c99, how is it possible
to declare a file scope object with internal linkage
without using 'static'?
 
K

Keith Thompson

j0mbolar said:
Which sections in the standard mandate that 'foo' below does
not qualify as a function that includes a prototype:

void foo(a)
char *a;
{
do_something(a);
}

int main(void)
{
foo();
return 0;
}

Probably the definition of "function prototype" in 6.2.1p2:

A _function prototype_ is a declaration of a function that
declares the types of its parameters.

The *definition* of foo() declares the type of a; the *declaration*
doesn't.
also, under 6.11.2 of c99, how is it possible
to declare a file scope object with internal linkage
without using 'static'?

I don't know. Anybody else?
 
J

j0mbolar

Keith said:
Probably the definition of "function prototype" in 6.2.1p2:

A _function prototype_ is a declaration of a function that
declares the types of its parameters.

The *definition* of foo() declares the type of a; the *declaration*
doesn't.

So where default argument promotions do not apply,
do pre-ansi compilers infer the type in the above example
from 'a', so that they know it is a 'pointer to char'?
 
K

Keith Thompson

j0mbolar said:
So where default argument promotions do not apply,
do pre-ansi compilers infer the type in the above example
from 'a', so that they know it is a 'pointer to char'?

Within the function, the declaration of a is visible, so the compiler
knows it's a char*.

For a call, the compiler doesn't know what type of argument the
function expects, so it's up to the caller to pass the right type.
It's similar to what happens with printf-like functions.
 
E

Eric Sosman

j0mbolar wrote On 02/07/06 20:41,:
Which sections in the standard mandate that 'foo' below does
not qualify as a function that includes a prototype:

void foo(a)
char *a;
{
do_something(a);
}

int main(void)
{
foo();
return 0;
}



also, under 6.11.2 of c99, how is it possible
to declare a file scope object with internal linkage
without using 'static'?

Perhaps 6.11.2 is referring to the situation described
in 6.2.2/4:

/* at file scope: */
static int x; /* internal linkage */
...
extern int x; /* internal linkage, obscolescent(?) */
 
J

j0mbolar

Eric said:
j0mbolar wrote On 02/07/06 20:41,:

Perhaps 6.11.2 is referring to the situation described
in 6.2.2/4:

/* at file scope: */
static int x; /* internal linkage */
...
extern int x; /* internal linkage, obscolescent(?) */

What would make that obsolescent? And technically,
this would involve the use of 'static' in making it
have internal linkage. It is just that the latter declaration
does not inhibit the former's meaning.
 
J

j0mbolar

Eric said:
j0mbolar wrote On 02/07/06 20:41,:

Perhaps 6.11.2 is referring to the situation described
in 6.2.2/4:

/* at file scope: */
static int x; /* internal linkage */
...
extern int x; /* internal linkage, obscolescent(?) */

I meant to set follow-ups to comp.std.c

What do the committee members in comp.std.c
have to say about this?
 
D

Douglas A. Gwyn

Does it *look* to you like there is a prototype in scope?
There is an argument mismatch in any event.
What do the committee members in comp.std.c
have to say about this?

Pretty much what Eric said, except we were thinking of
it without the explicit "extern" (same thing).
 
A

Antoine Leca

En news:[email protected], Douglas A. Gwyn va escriure:
Pretty much what Eric said, except we were thinking of
it without the explicit "extern" (same thing).

Can you elaborate?

If you mean
static int x;
int x;
I read 6.2.2p5, 2nd sentence as making x of external linkage, and the
combination of both lines is UB by 6.2.2p7.

This text is unchanged since C89 (I thought a DR had added something, so
checked, and turned empty.)


Antoine
 
A

Antoine Leca

En j0mbolar va escriure:
What would make that obsolescent?

What: the Standard, the very 6.11.2.

Why: because when you are looking only at the second line (arbitrarily far
from the first), you do not immediately notice that the identifier is not
visible outside this translation unit.

Making this obsolescent is (was really) a free card for the committee to
revoke such a possibility in the future. It is also a strong hint the above
is "bad style."


Antoine
 
K

kuyper

You should be aware that the following counts as a declaration of
foo():

while everything from here on in is only part of the definition of
foo(); it's not part of the declaration.
A declaration that is only a declaration, and not a definition, could
be present in one translation unit, while the actual definition of the
function might be in an entirely different translation unit.
So where default argument promotions do not apply,
do pre-ansi compilers infer the type in the above example
from 'a', so that they know it is a 'pointer to char'?

No; they handled such code in pretty much the same way as it's
currently handled. The definition of 'a' that is provided as part of
the definition of foo() has significance only inside the definition of
foo(); it has no effect on the interpretation of calls to foo(). If the
promoted type of an argument is incompatible with the actual type of
the corresponding parameter, you've got trouble; there's no implicit
conversion to the specified type, because in the absence of a
prototype, there is no specified type. There's also no mandatory
diagnostic to warn you of the problem. That's why prototypes were
invented.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top