Use of extern

P

polas

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

Then in a file called e.c, a small snapshot I have

int a;
void hello(int num)
{
.....
}

These two C files are linked together during compilation, but is there
any reason to add extern in front of the hello function prototype in
a.c (or would adding this not conform to the standard.) On my C
compiler (gcc) it doesnt complain when I add extern in this way, but
doesnt seem to do a huge amount.

Cheers,
Nick
 
N

Nick Keighley

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?

sort of
For instance, I have a file called a.c - a small snapshot of it
contains

extern int a;
void hello(int);

Then in a file called e.c, a small snapshot I have

int a;
void hello(int num)
{
....

}

These two C files are linked together during compilation, but is there
any reason to add extern in front of the hello function prototype

only for documentaion reasons
in
a.c (or would adding this not conform to the standard.)

adding extern to a function declaration does conform to
the standard

On my C
compiler (gcc) it doesnt complain when I add extern in this way, but
doesnt seem to do a huge amount.

correct.

function declaration
void hello(int);

function definition:
void hello(int n)
{
}

by default a function definition is has external linkage
(is "visible" throughout the program) if you put static
on the front it only has file scope (it is only "visible"
to the file it is definied in). Adding extern is the same as
not using static. Hence some people use extern on functions
and many do not. I don't use extern on functions.
 
B

Ben Bacarisse

polas said:
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?

Yes, but extern is the default for file-scope function declarations.
It is usual to omit it. You need it if you write a function
declaration at block scope but that is not commonly done.
 

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,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top