Connecting Tech Pros Worldwide Help | Site Map

extern

Bill Cunningham
Guest
 
Posts: n/a
#1: Sep 9 '08
Is extern used to externalize functions as well as variables? If I have
this function,

int num(int n);

Should it be declared as above in the file in which main is called and
compiled with the file that contains num's body? Or should it be declared as
this,

extern num (int n);

Bill


Bill Cunningham
Guest
 
Posts: n/a
#2: Sep 9 '08

re: extern


I just don't quite understand extern. I am wanting to put functions in
file and compile as object files and link. Is a header required?

Bill


Stephen Sprunk
Guest
 
Posts: n/a
#3: Sep 9 '08

re: extern


Bill Cunningham wrote:
Quote:
Is extern used to externalize functions as well as variables? If I have
this function,
>
int num(int n);
>
Should it be declared as above in the file in which main is called and
compiled with the file that contains num's body? Or should it be declared as
this,
>
extern num (int n);
The usual convention is that you would put the declaration ("int num(int
n);") in a header (.h) file and then include that header file into any
source (.c) file that called that function plus the source file that
actually defined it.

"extern" is the default for function declarations, so it's not
necessary. If you wanted to share a variable across multiple source
files (a "global" variable), though, you would need the "extern" on its
declaration in the header file.

S
Bill Cunningham
Guest
 
Posts: n/a
#4: Sep 9 '08

re: extern



"Stephen Sprunk" <stephen@sprunk.orgwrote in message
news:_dAxk.589$Z64.389@flpi143.ffdc.sbc.com...
Quote:
"extern" is the default for function declarations, so it's not necessary.
If you wanted to share a variable across multiple source files (a "global"
variable), though, you would need the "extern" on its declaration in the
header file.
I see. But if you declared a variable in a header shared by several
source files without extern, would it be global?

Bill


Harald van =?UTF-8?b?RMSzaw==?=
Guest
 
Posts: n/a
#5: Sep 9 '08

re: extern


On Tue, 09 Sep 2008 15:20:10 -0400, Bill Cunningham wrote:
Quote:
Is extern used to externalize functions as well as variables?
No.
Quote:
If I have this function,
>
int num(int n);
>
Should it be declared as above in the file in which main is called and
compiled with the file that contains num's body? Or should it be
declared as this,
>
extern num (int n);
No. Why did you decide to remove "int"?

You can declare it as either "int num(int n);" or
"extern int num(int n);", either in a header file or directly in the file
where you want to call num. "extern" doesn't mean anything extra here.
Bill Cunningham
Guest
 
Posts: n/a
#6: Sep 9 '08

re: extern



"Harald van D?k" <truedfx@gmail.comwrote in message
news:8a6b2$48c6d35b$541ad5a0$20514@cache5.tilbu1.n b.home.nl...
Quote:
No. Why did you decide to remove "int"?
Sorry typo.

Bill


Stephen Sprunk
Guest
 
Posts: n/a
#7: Sep 9 '08

re: extern


Bill Cunningham wrote:
Quote:
"Stephen Sprunk" <stephen@sprunk.orgwrote in message
news:_dAxk.589$Z64.389@flpi143.ffdc.sbc.com...
Quote:
>"extern" is the default for function declarations, so it's not necessary.
>If you wanted to share a variable across multiple source files (a "global"
>variable), though, you would need the "extern" on its declaration in the
>header file.
>
I see. But if you declared a variable in a header shared by several
source files without extern, would it be global?
If you did not declare the variable "extern", you would likely end up
with separate copies of the variable (using the same name) for each file
that included your header. That is not good if you want a single
"global" variable shared across all those source files.

S
Amandil
Guest
 
Posts: n/a
#8: Sep 11 '08

re: extern


On Sep 9, 4:39*pm, Stephen Sprunk <step...@sprunk.orgwrote:
Quote:
Bill Cunningham wrote:
Quote:
"Stephen Sprunk" <step...@sprunk.orgwrote in message
news:_dAxk.589$Z64.389@flpi143.ffdc.sbc.com...
Quote:
"extern" is the default for function declarations, so it's not necessary.
If you wanted to share a variable across multiple source files (a "global"
variable), though, you would need the "extern" on its declaration in the
header file.
>
Quote:
* * I see. But if you declared a variable *in a header shared by several
source files without extern, would it be global?
>
If you did not declare the variable "extern", you would likely end up
with separate copies of the variable (using the same name) for each file
that included your header. *That is not good if you want a single
"global" variable shared across all those source files.
>
S
Worse, it would give you a link error, for finding several global
variables with the same name.

-- Marty
Nick Keighley
Guest
 
Posts: n/a
#9: Sep 12 '08

re: extern


On 9 Sep, 20:49, Harald van D©¦k <true...@gmail.comwrote:
Quote:
On Tue, 09 Sep 2008 15:20:10 -0400, Bill Cunningham wrote:
Quote:
Quote:
Is extern used to externalize functions as well as variables?
>
No.
? I'd have said "yes"

Quote:
Quote:
If I have this function,
>
Quote:
int num(int n);
>
Quote:
Should it be declared as above in the file in which main is called and
compiled with the file that contains num's body? Or should it be
declared as this,
>
Quote:
extern num (int n);
>
No. Why did you decide to remove "int"?
>
You can declare it as either "int num(int n);" or
"extern int num(int n);", either in a header file or directly in the file
where you want to call num. "extern" doesn't mean anything extra here.
I don't actually use extern on functions as by default they are
extern.
If you want to confine them to a single file use static.

Bill, try reading section 4.3 of K&R 2e. Though for once I found K&R
a bit heavy going in this area.

Unfortunatly ANSI had a bit of a job here. They try to explain
both what you should do (best practice) and what you can do
(so as not to break existing code)


--
Nick Keighley

quark: The sound made by a well bred duck.
Richard
Guest
 
Posts: n/a
#10: Sep 12 '08

re: extern


Harald van Dijk <truedfx@gmail.comwrites:
Quote:
On Tue, 09 Sep 2008 15:20:10 -0400, Bill Cunningham wrote:
Quote:
>Is extern used to externalize functions as well as variables?
>
No.
>
Quote:
>If I have this function,
>>
>int num(int n);
>>
>Should it be declared as above in the file in which main is called and
>compiled with the file that contains num's body? Or should it be
>declared as this,
>>
>extern num (int n);
>
No. Why did you decide to remove "int"?
>
You can declare it as either "int num(int n);" or
"extern int num(int n);", either in a header file or directly in the file
where you want to call num. "extern" doesn't mean anything extra here.
In a header file which is included by files which want to use "num()" it
would be quite common to see num() defined as

extern int num(int n);

C isn't strict enough in terms of these things IMO.

Declaring a function inside the file in which it is actually defined is,
IMO ridiculous half the time and totally unnecessary.
Harald van =?UTF-8?b?RMSzaw==?=
Guest
 
Posts: n/a
#11: Sep 12 '08

re: extern


On Fri, 12 Sep 2008 02:40:31 -0700, Nick Keighley wrote:
Quote:
On 9 Sep, 20:49, Harald van Dijk <true...@gmail.comwrote:
Quote:
>On Tue, 09 Sep 2008 15:20:10 -0400, Bill Cunningham wrote:
Quote:
Is extern used to externalize functions as well as variables?
>>
>No.
>
? I'd have said "yes"
I meant it doesn't change the meaning of the function declaration: it
doesn't actually "externalize" the function, since it is "externalized"
already without it.
Closed Thread


Similar C / C++ bytes