This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: (extern int vs. int) and (extern function vs. function) (was: gcc-in-cxx update)
Eus <reply.to.eus.at.member.fsf.org@gmail.com> writes:
> I think the difference between "int i;" and "extern int i;" at
> file-scope in C is that "int i;" will only be treated as a definition if
> it is not defined in another place in the same file/TU. IOW, its linkage
> is internal within the TU itself. But, "extern int i" is definitely
> treated as a declaration only that can later be defined either in the
> same TU or in another one.
>
> Is that true?
What you are describing is a common and traditional implementation of C,
but it is not strictly standard conformant. The ISO C standard says
that "int i;" is always a definition, and "extern int i;" is always a
declaration. What you decribe is listed as a common extension (at least
in C90--I didn't bother to check C99).
> Finally, is there any difference in declaring a non-inline function
> prototype with and without "extern" in C? That is, is there any
> difference between:
>
> extern int foo (int *bar);
>
> and
>
> int foo (int *bar);
>
> ?
>
> I think they are completely identical. Is that correct?
Yes. In a function (not variable) declaration, the "extern" is
optional. This is true in both C and C++.
> Also, is there any difference in defining a non-inline function with and
> without "extern" in C? That is, is there any difference between:
>
> extern int foo (int *bar)
> {
> return *bar + 5;
> }
>
> and
>
> int foo (int *bar)
> {
> return *bar + 5;
> }
>
> ?
>
> I think they are also identical. Am I right? Who knows the one with
> "extern" can be overridden in another TU defining its own definition
> without "extern".
These are not identical, but the meaning in C is complex because it
changes depending on which standard you are using. Look at the docs for
the -fgnu89-inline option and the gnu_inline function attribute.
Ian