[comp.std.c] Declarations with more/less detail, or "static" functins.

ORCUTT@cc.utah.edu (08/11/90)

I want to place a declaration for a static function at
the beginning of a file and define the function later
in the file.  What is the ANSI way to do this?  If I write

static void func(void);     /* Declaration. */

..

static void func(void)     /* Definition. */
{
..
return;
}

H & S seem to indicate that the first "static" is 
illegal.  If I remove it, the declaration will default
to type "extern", causing a conflict with the function definition.

In practice, MSC 5.1 and TC 2.0 and 3.0 allow the "static" on
the declaration.  If I omit the "static" on the definition,
these compilers will also declare the function "static" from
the previous declaration.  On the other hand, High-C follows
H & S more strictly in that it changes the "static" on the
declaration to "extern", with a warning.  However, High-C
allows the "static" on the definition to override the "extern"
assumed from the declaration, which also seems to be
non-standard behavior.

Is there an ANSI approved way to declare a function that
will be "static" in a file before it is defined?  This is
useful to that functions earlier in the file can reference
the function with the correct type of return value and
prototype.

The High-C way seems more in tune with the standard, which
states that a declaration can "add detail" to an earlier one;
but the High-C compiler prints about 5 lines of diagnostic
messages when it sees the "static" on the function definition,
although it does the right thing.

Does the standard allow one to make a declaration with
less detail of an object after it has been declared
with greater detail?  For example, are the declarations

double cos(double x);

and

double cos();

compatible?

Thanks for your attention to my ramblings...

gwyn@smoke.BRL.MIL (Doug Gwyn) (08/13/90)

In article <84313@cc.utah.edu> ORCUTT@cc.utah.edu writes:
>static void func(void);     /* Declaration. */
>..
>static void func(void)     /* Definition. */
>{
>..
>H & S seem to indicate that the first "static" is illegal.

No, it's fine.

>If I remove it, the declaration will default to type "extern", ...

Yes, assuming that there is not another static-linkage declaration in scope.

>If I omit the "static" on the definition, these compilers will also
>declare the function "static" from the previous declaration.

Yes, also the standard requires that if there is an explicit "extern".
"extern" doesn't really mean "external linkage"; it means more "external
linkage if not already known to have static linkage".  (For objects rather
than functions, this is true for explicit "extern" but not for omitted
storage class.)  This mess has historical origins.  At least section 3.1.2.2
of the C standard is quite explicit about the official linkage rules.

>Is there an ANSI approved way to declare a function that
>will be "static" in a file before it is defined?

Sure; include the "static" storage-class specifier.

>The High-C way seems more in tune with the standard, which
>states that a declaration can "add detail" to an earlier one;

? The standard does not say that.

>... are the declarations
>double cos(double x);
>and
>double cos();
>compatible?

Yes, in the sense that having them both in scope does not violate any
constraint of the standard.  In the case of cos(), #including <math.h>
is the recommended way to declare the function.