[Q]extern in front of function definition? | | |
Hi,
BSD code has following hcreate function definition in hsearch.c
under lib/libc/db/hash/hsearch.c.
Here, why is 'extern' used ? What's the purpose of it? How come
in infront of functions definition ? ( Usually 'extern' is placed in
front of function declaration....)
extern int
hcreate(nel)
size_t nel;
{
HASHINFO info;
info.nelem = nel;
info.bsize = 256;
info.ffactor = 8;
info.cachesize = 0;
info.hash = NULL;
info.lorder = 0;
dbp = (DB *)__hash_open(NULL, O_CREAT | O_RDWR, 0600, &info, 0);
return (dbp != NULL);
}
.... | | | | re: [Q]extern in front of function definition? djhong@gmail.com said: Quote:
Hi,
>
BSD code has following hcreate function definition in hsearch.c
under lib/libc/db/hash/hsearch.c.
Here, why is 'extern' used ?
No real reason. Quote:
What's the purpose of it?
Someone liked typing, I guess. Quote:
How come
in infront of functions definition ?
It means that the function has external linkage (which it has anyway by
default). Quote:
( Usually 'extern' is placed in
front of function declaration....)
Definitions are also declarations.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www. | | | | re: [Q]extern in front of function definition?
Richard Heathfield <r...@see.sig.invalidwrote: Quote:
djh...@gmail.com said: Quote:
BSD code has following hcreate function definition in hsearch.c
under lib/libc/db/hash/hsearch.c.
Here, why is 'extern' used ?
>
No real reason.
> Quote:
What's the purpose of it?
>
Someone liked typing, I guess.
I recall one chap on clc insists on redundantly initialising
_all_ automatic variables. Some people have strange notions of
what's real... ;-)
--
Peter | | | | re: [Q]extern in front of function definition?
Peter Nilsson said: Quote:
Richard Heathfield <r...@see.sig.invalidwrote: Quote:
>djh...@gmail.com said: Quote:
BSD code has following hcreate function definition in hsearch.c
under lib/libc/db/hash/hsearch.c.
Here, why is 'extern' used ?
>>
>No real reason.
>> Quote:
What's the purpose of it?
>>
>Someone liked typing, I guess.
>
I recall one chap on clc insists on redundantly initialising
_all_ automatic variables. Some people have strange notions of
what's real... ;-)
<grinWell, I have my reasons, and they are well known here. Can you
come up with a plausible reason for extern-qualifying functions?
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www. | | | | re: [Q]extern in front of function definition?
Richard Heathfield <rjh@see.sig.invalidwrites: Quote:
Peter Nilsson said: Quote:
>Richard Heathfield <r...@see.sig.invalidwrote: Quote:
>>djh...@gmail.com said:
>BSD code has following hcreate function definition in hsearch.c
>under lib/libc/db/hash/hsearch.c.
>Here, why is 'extern' used ?
>>>
>>No real reason.
>>>
>What's the purpose of it?
>>>
>>Someone liked typing, I guess.
>>
>I recall one chap on clc insists on redundantly initialising
>_all_ automatic variables. Some people have strange notions of
>what's real... ;-)
>
<grinWell, I have my reasons, and they are well known here. Can you
come up with a plausible reason for extern-qualifying functions?
As a matter of fact, I can.
In general, functions should be declared as static unless there's a
specific requirement for them to be visible outside the current
translation unit. Explicitly declaring externally visible functions
as "extern", though it doesn't do anything for the compiler, acts as
an explicit reminder to the reader that the function really is
intended to be externally visible, rather than just being accidentally
visible because that's the default.
(I'm not sure I believe that, but I do think its plausible.)
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister" | | | | re: [Q]extern in front of function definition?
Keith Thompson said: Quote:
Richard Heathfield <rjh@see.sig.invalidwrites: Quote:
>Can you
>come up with a plausible reason for extern-qualifying functions?
>
As a matter of fact, I can.
>
In general, functions should be declared as static unless there's a
specific requirement for them to be visible outside the current
translation unit. Explicitly declaring externally visible functions
as "extern", though it doesn't do anything for the compiler, acts as
an explicit reminder to the reader that the function really is
intended to be externally visible, rather than just being accidentally
visible because that's the default.
>
(I'm not sure I believe that, but I do think its plausible.)
I'm impressed. It is indeed a plausible reason.
<SFX: removes hat>
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www. | | | | re: [Q]extern in front of function definition?
In article <lnk5x3hztu.fsf@nuthaus.mib.org>,
Keith Thompson <kst-u@mib.orgwrote: Quote:
>In general, functions should be declared as static unless there's a
>specific requirement for them to be visible outside the current
>translation unit. Explicitly declaring externally visible functions
>as "extern", though it doesn't do anything for the compiler, acts as
>an explicit reminder to the reader that the function really is
>intended to be externally visible, rather than just being accidentally
>visible because that's the default.
It also saves you from having to add the "extern" when you copy it
to the .h file.
-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963. | | | | re: [Q]extern in front of function definition?
Richard Heathfield wrote On 03/27/07 03:10,: Quote:
Peter Nilsson said:
>
> Quote:
>>Richard Heathfield <r...@see.sig.invalidwrote:
>> Quote:
>>>djh...@gmail.com said:
>>>
>>>>BSD code has following hcreate function definition in hsearch.c
>>>>under lib/libc/db/hash/hsearch.c.
>>>>Here, why is 'extern' used ?
>>>
>>>No real reason.
>>>
>>>
>>>>What's the purpose of it?
>>>
>>>Someone liked typing, I guess.
>>
>>I recall one chap on clc insists on redundantly initialising
>>_all_ automatic variables. Some people have strange notions of
>>what's real... ;-)
>
>
<grinWell, I have my reasons, and they are well known here. Can you
come up with a plausible reason for extern-qualifying functions?
"Functions" in general, no. But "this function," yes!
Observation #1: The function is defined without a prototype,
using a K&R-style argument list.
Observation #2: The function is described as being part of
the BSD source.
Recollection: BSD's origins antedate the first C Standard.
Plausible supposition: This function was written to be
compiled by pre-Standard compilers, in the wild and wooly days
when every compiler was its own language definition. Some of
those compilers may have done Something Good (or avoided doing
Something Bad) with the now-useless extern.
Counter-indications: The use of size_t suggests more recent
provenance, as do the "untagged" names of the struct elements.
Cavalier dismissal of counter-indications: One or more
programmers have visited the source since its original writing,
and have brought it incompletely up-to-date.
-- Eric.Sosman@sun.com | | | | re: [Q]extern in front of function definition?
Eric Sosman said: Quote:
Some of
those compilers may have done Something Good (or avoided doing
Something Bad) with the now-useless extern.
Eric - My solicitor will be in touch with you about these trademark
violations. You might want to get your chequebook ready.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www. | | | | re: [Q]extern in front of function definition?
Richard Tobin wrote, On 27/03/07 11:12: Quote:
In article <lnk5x3hztu.fsf@nuthaus.mib.org>,
Keith Thompson <kst-u@mib.orgwrote:
> Quote:
>In general, functions should be declared as static unless there's a
>specific requirement for them to be visible outside the current
>translation unit. Explicitly declaring externally visible functions
>as "extern", though it doesn't do anything for the compiler, acts as
>an explicit reminder to the reader that the function really is
>intended to be externally visible, rather than just being accidentally
>visible because that's the default.
>
It also saves you from having to add the "extern" when you copy it
to the .h file.
It does not save me having to do that since C does not require it. Of
course, if your coding standard does...
--
Flash Gordon | | | | re: [Q]extern in front of function definition?
In article <pl5od4x064.ln2@news.flash-gordon.me.uk>,
Flash Gordon <spam@flash-gordon.me.ukwrote: Quote: Quote: Quote:
>>In general, functions should be declared as static unless there's a
>>specific requirement for them to be visible outside the current
>>translation unit. Explicitly declaring externally visible functions
>>as "extern", though it doesn't do anything for the compiler, acts as
>>an explicit reminder to the reader that the function really is
>>intended to be externally visible, rather than just being accidentally
>>visible because that's the default.
Quote: Quote:
>It also saves you from having to add the "extern" when you copy it
>to the .h file.
Quote:
>It does not save me having to do that since C does not require it. Of
>course, if your coding standard does...
Hmm, that's true. I think I just do it out of some habit acquired in
the mists of time.
-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963. | | | | re: [Q]extern in front of function definition?
On 27 Mar 2007 23:49:03 GMT, richard@cogsci.ed.ac.uk (Richard Tobin)
wrote: Quote:
>In article <pl5od4x064.ln2@news.flash-gordon.me.uk>,
>Flash Gordon <spam@flash-gordon.me.ukwrote:
> Quote: Quote:
>>>In general, functions should be declared as static unless there's a
>>>specific requirement for them to be visible outside the current
>>>translation unit. Explicitly declaring externally visible functions
>>>as "extern", though it doesn't do anything for the compiler, acts as
>>>an explicit reminder to the reader that the function really is
>>>intended to be externally visible, rather than just being accidentally
>>>visible because that's the default.
> Quote: Quote:
>>It also saves you from having to add the "extern" when you copy it
>>to the .h file.
> Quote:
>>It does not save me having to do that since C does not require it. Of
>>course, if your coding standard does...
>
>Hmm, that's true. I think I just do it out of some habit acquired in
>the mists of time.
>
Hmm here, too. I don't use extern in headers, but I use it when I add
a single prototype for an external function. Same reason as you, I
guess.
--
Al Balmer
Sun City, AZ |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,327 network members.
|