469,604 Members | 2,377 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,604 developers. It's quick & easy.

extern and static

Hello all,
Under C++ when 'extern' is used with out the string literal "C" does
it act the same as a 'static' ? When is it appropriate using 'extern' ?

Jul 22 '05 #1
2 2107
ik posted:
Hello all,
Under C++ when 'extern' is used with out the string literal "C" does it act the same as a 'static' ? When is it appropriate using 'extern' ? Thanks

When you write:

int k;

Then that's going to define a variable.

But what if you don't want to define a variable, what if
this variable has already been defined in some other source
file and you just want to use it in this source file. Well
here's how you say that you *don't* want to define a
variable, you just want to use one that's already been
defined (sort of like a function prototype):

extern int k;
Jul 22 '05 #2
ik wrote in news:10**********************@k17g2000odb.googlegr oups.com in
Hello all,
Under C++ when 'extern' is used with out the string literal "C" does
it act the same as a 'static' ?
No extern is used to declare that something is defined elsewhere possibly
in another TU (TU = Translation Unit, a source file or library etc).

static has several meanings:

1) Used in a class/struct/union it declares/defines that the class
(not its instances) has a member object or function.

2) Used at namespace scope (*) or within a function it declares/defines
an object or function (namespace scope only) that has internal
linkage, i.e. other TU's (sources) will not be able to access
this object (as it doesn't have an "extern" name).

Within a function it also means the object will live from the
time it is first initialized (when the function is first called)
to the end of the programme.

*) namespace scope: within a namespace block:

namespace name { /* here */ }

or at global scope, not in a namespace, class or function.
When is it appropriate using 'extern' ?

extern is mostly useful for objects that you want to declare but
not define:

int x;
extern int y;

In the above x is declared and defined, y is only declared, so
in another TU (source) y could be defined:

int y = 2;

However both x and y have external linkage, its the default.

extern also needs to be used with const's that you want to have
external linkage, as const object's have internal linkage by
default (i.e. they are static):

int const a = 3;
extern int const b;
extern int const c = 4;

In the above a has internal linkage, b has external linkage
and will need to be defined elsewhere:

extern int const b = 2;

and c has external linkage, but because it has an intializer
it is also a defenition.

Declaration's are extern by default so, with function's:

int f();

is equivalent to:

extern int f();

The only time you need to use extern with a function is
when you want to give it a linkage specification:

extern "C" int g();


Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Mark A. Gibbs | last post: by
18 posts views Thread by tweak | last post: by
19 posts views Thread by ccwork | last post: by
17 posts views Thread by Tapeesh | last post: by
4 posts views Thread by Sean | last post: by
5 posts views Thread by Christian Christmann | last post: by
7 posts views Thread by ruffiano | last post: by
3 posts views Thread by coder | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.