473,805 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[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(N ULL, O_CREAT | O_RDWR, 0600, &info, 0);
return (dbp != NULL);
}
....

Mar 26 '07 #1
11 3366
dj****@gmail.co m said:
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.
What's the purpose of it?
Someone liked typing, I guess.
How come
in infront of functions definition ?
It means that the function has external linkage (which it has anyway by
default).
( 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.
Mar 26 '07 #2
Richard Heathfield <r...@see.sig.i nvalidwrote:
djh...@gmail.co m 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... ;-)

--
Peter

Mar 27 '07 #3
Peter Nilsson said:
Richard Heathfield <r...@see.sig.i nvalidwrote:
>djh...@gmail.c om 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?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 27 '07 #4
Richard Heathfield <rj*@see.sig.in validwrites:
Peter Nilsson said:
>Richard Heathfield <r...@see.sig.i nvalidwrote:
>>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_Keit h) ks***@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"
Mar 27 '07 #5
Keith Thompson said:
Richard Heathfield <rj*@see.sig.in validwrites:
>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.
Mar 27 '07 #6
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.orgw rote:
>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
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 27 '07 #7
Richard Heathfield wrote On 03/27/07 03:10,:
Peter Nilsson said:

>>Richard Heathfield <r...@see.sig.i nvalidwrote:
>>>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.

--
Er*********@sun .com
Mar 27 '07 #8
Eric Sosman said:
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.
Mar 27 '07 #9
Richard Tobin wrote, On 27/03/07 11:12:
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.orgw rote:
>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
Mar 27 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
4293
by: tweak | last post by:
What's the best way to use extern when using multiplefiles that is easiest to maintain? Is it best to declare: extern int a; in a header file and include the header file in all files except where it's defined.
19
3868
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default storage-class specifier. My (little) C experience tells me that an object with "extern" is to let the linker knows that the object is referencing the object defined in somewhere, and this "somewhere" object does not have the storage-class specifier...
29
448
by: DevarajA | last post by:
Can anyone explain me what extern is used for? I thought it was used to declare variables definited in other files, but i can do that also without extern. /*file a.c*/ int a=5; int main() { f(); }
5
16611
by: siliconwafer | last post by:
Hi all, I wanted to know that is use of extern keyword mandatory in case of global variables and functions used in other source files? i.e consider a following piece of code from MSDN explaining extern storage class: /****************************************************************** SOURCE FILE ONE *******************************************************************/ extern int i; /* Reference to i, defined below */
17
4936
by: Tapeesh | last post by:
I would like to know what is the expected behaviour of C compilers when an extern decleration is intialized. When the following code is compiled using gcc //File extern.c int arr ; int a ;
5
2862
by: Christian Christmann | last post by:
Hi, I've tree questions on the storage class specifier "extern": 1) Code example: int main( void ) { int b = -2; // my line 3 if ( a ) {
16
2326
by: Martin Jørgensen | last post by:
Hi, Problem: ======== Some of my output functions are beginning to take pretty many arguments... I mean.... We're talking about 10-15 arguments :-) So I thought to myself, perhaps this is beginning to get out of hands if I continue to put in extra functionality in the (file-writing) output-functions....
7
8320
by: Travis | last post by:
I'm relatively new to externs and function pointers but have inherited the task of modifying some existing code. I have something like this. extern void myExternFunc (MenuItem *item, void * param) void (*fptr)(MenuItem *item, void * param) = myExternFunc; This doesn't compile. I don't understand why. Obviously something like this: extern void myFunc (MenuItem *item, void * param) { }
3
2032
by: polas | last post by:
I have a quick question - I have used extern quite a bit in relation to variables, however was wondering if its applicable to function prototypes too? For instance, I have a file called a.c - a small snapshot of it contains extern int a; void hello(int);
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10609
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10360
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10366
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10105
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5542
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3845
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.