extern needed or not?

L

Laurijssen

Is it needed to use the keyword extern in headers when you want a function
to have external linkage? Or can I leave it out?

/// header.h
int func(void);
or
extern int func(void);

//header.c
#include "header.h"

int main(void)
{
func();
return 0;
}
 
B

Ben Pfaff

Laurijssen said:
Is it needed to use the keyword extern in headers when you want a function
to have external linkage?
No.

Or can I leave it out?

Functions have external linkage by default.
 
L

Laurijssen

Ben Pfaff said:
Functions have external linkage by default.

Well I read in K&R that when extern is omitted it's a temporary declaration.
Only after the compiler sees an extern declaration its final and the
compiler issues an error message when the signature of the function changes
in another delaration. I thought maybe it's a standard C thing or just K&R
 
M

Micah Cowan

Laurijssen said:
Well I read in K&R that when extern is omitted it's a temporary declaration.
Only after the compiler sees an extern declaration its final and the
compiler issues an error message when the signature of the function changes
in another delaration. I thought maybe it's a standard C thing or just K&R

I think you're confused. For /object/ types (not functions), a
file-scope declaration without a linkage specifier, or with the
"static" linkage specifier, /and/ without an initializer, is called a
"tentative" definition. Basically, it allows you to fill in more
information later.

For instance, if want to define a file-scope variable "int_value",
it's allowed to define it like this:

static int int_value;

and use it from within function bodies, etc., and then later on in the
same translation unit you could give it an explicit initial value:

static int int_value = 100;

This will make it have the value 100 before it ever gets used by any
of the functions, and also removes the "tentativity" of the
definition.

But, even if you never explicitly clarify the tentative definition,
the details will still be filled in implicitly. Initializers are given
their "default initializer" (basically, zeroes). Definitions like:

int foo[];

become:

int foo[1] = {0};
 
V

Vladimir S. Oka

Laurijssen opined:

You do need a bit more context... Care to provide it? If memory serves
it was K&R, but I can't track the post down at the moment.
 
P

pete

Laurijssen said:

I meant one that backs you up,
not one that contradicts you directly.

Ben Pfaff wrote: "Functions have external linkage by default."

You replied : "Well I read in K&R that when extern is
omitted it's a temporary declaration."

A10.2 says : "If the first external declaration for a function
or object includes the static specifier,
the identifier has internal linkage;
otherwise it has external linkage."

That agrees with what Ben Pfaff said.

A10.2 uses the term "tentative defintion"
in text pertaining to objects.
Ben Pfaff was not discussing objects.
Your use of the word "it" indicates
that you were not refering to objects either.
 
L

Laurijssen

pete said:
I meant one that backs you up,
not one that contradicts you directly.

Ben Pfaff wrote: "Functions have external linkage by default."

You replied : "Well I read in K&R that when extern is
omitted it's a temporary declaration."

A10.2 says : "If the first external declaration for a function
or object includes the static specifier,
the identifier has internal linkage;
otherwise it has external linkage."

That agrees with what Ben Pfaff said.

A10.2 uses the term "tentative defintion"
in text pertaining to objects.
Ben Pfaff was not discussing objects.
Your use of the word "it" indicates
that you were not refering to objects either.

I have dutch translated book and I cant read it now. Would help I'd say ;)
 

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,774
Messages
2,569,599
Members
45,167
Latest member
SusanaSwan
Top