473,320 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

extern and static

ik
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
~Ik

Jul 22 '05 #1
2 2414
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
~Ik


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;
-JKop
Jul 22 '05 #2
ik wrote in news:10**********************@k17g2000odb.googlegr oups.com in
comp.lang.c++:
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();

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: //...
18
by: tweak | last post by:
What's the best way to use extern when using multiplefiles that is easiest to maintain? Is it best to declare: extern int a; in a header file and include the header file in all files except...
19
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default...
17
by: Tapeesh | last post by:
I would like to know what is the expected behaviour of C compilers when an extern decleration is intialized. When the following code is compiled using gcc //File extern.c int arr ; int a ;
4
by: Sean | last post by:
I am a little confused by the "extern inline and static inline" rules. I understand that "extern inline" guarantees no function storage is created (all are inlined). But the following test seems to...
5
by: Christian Christmann | last post by:
Hi, I've tree questions on the storage class specifier "extern": 1) Code example: int main( void ) { int b = -2; // my line 3 if ( a ) {
7
by: ruffiano | last post by:
I have a file called first.cpp that contains the following declarations: static const char *MY_STRING1 = "My first string"; static const char *MY_STRING2 = "My second string" If I wanted to use...
3
by: coder | last post by:
While reading the page on "Reading C Declarations" <http://www.ericgiguere.com/articles/reading-c-declarations.html> (recommended by Mr. Heathfield at...
5
by: Anonymous | last post by:
I have a class that needs to be accesed by a C API. I need to expose some private methods to the C API : #ifdef __cplusplus extern "C" #endif void peek(Object_Handle handle); void...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.