extern

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

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
 
B

Bill Cunningham

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
 
S

Stephen Sprunk

Bill said:
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
 
B

Bill Cunningham

"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
 
H

Harald van Dijk

Is extern used to externalize functions as well as variables?
No.

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.
 
S

Stephen Sprunk

Bill said:
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
 
A

Amandil

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
 
N

Nick Keighley


? I'd have said "yes"

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)
 
R

Richard

Harald van Dijk said:
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.
 
H

Harald van Dijk

? 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top