473,769 Members | 6,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2444
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.goo glegroups.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
6196
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: // main.cpp // This is defined in a C module extern "C" void fake_qsort(void*, std::size_t, std::size_t, int (*compare)(const void*, const void*));
18
4288
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 where it's defined.
19
3860
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 storage-class specifier. My (little) C experience tells me that an object with "extern" is to let the linker knows that the object is referencing the object defined in somewhere, and this "somewhere" object does not have the storage-class specifier...
17
4933
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
7244
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 contradict what I expect---I found inc_i() is actually compiled as a linkable function in the a.out >g++ main.cpp call_inc.cpp >nm a.out ...... 0804875e W _Z5inc_iv
5
2859
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
454
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 MY_STRING1 and MY_STRING2 in a header file, would the following be correct? extern static const char *MY_STRING1; extern static const char *MY_STRING2;
3
6406
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 <http://www.cpax.org.uk/prg/portable/c/resources.php>), I came across the following declaration: extern char *const (*goop( char *b ))( int, long ); which is explained as:
5
5063
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 poke(Object_Handle handle);
0
9590
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10223
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10051
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10000
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.