473,703 Members | 2,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"extern" inside a block

I've seen some code with extern modifiers in front of variables
declared inside blocks. Are these purely definitions (no definition)
or are they definitions with static duration but external linkage?

Not much on this in the FAQ or tutorials.

Nov 14 '05 #1
12 2724

G Patel wrote:
I've seen some code with extern modifiers in front of variables
declared inside blocks. Are these purely definitions (no definition)
correction: purely declarations
or are they definitions with static duration but external linkage?

Not much on this in the FAQ or tutorials.


Nov 14 '05 #2


G Patel wrote:
G Patel wrote:
I've seen some code with extern modifiers in front of variables
declared inside blocks. Are these purely definitions (no definition)

correction: purely declarations

or are they definitions with static duration but external linkage?

Not much on this in the FAQ or tutorials.


They are declarations of objects or functions
defined elsewhere with external linkage. (IMHO they
are also stylistically repugnant, but that's another
discussion.)

--
Er*********@sun .com

Nov 14 '05 #3
G Patel wrote:
I've seen some code with extern modifiers
in front of variables declared inside blocks.
Are these purely [declarations] (no definition)
Yes.
or are they definitions with static duration but external linkage?
No.
Not much on this in the FAQ or tutorials.
Not surpizing.
There is seldom call for this.
I use it when I want to keep declarations private:
cat main.c

#include <stdio.h>

int main(int argc, char* argv[]) {
extern void* malloc(size_t);
extern void free(void*);
const size_t n = 13;
int* p = (int*)malloc(n* sizeof(int));
free((void*)p);
return 0;
}

When, for example, I want to use a private helper function.
Nov 14 '05 #4
Eric Sosman wrote:
G Patel wrote:
G Patel wrote:
I've seen some code with extern modifiers in front of variables
declared inside blocks. Are these purely definitions (no
definition)

correction: purely declarations

or are they definitions with static duration but external linkage?

Not much on this in the FAQ or tutorials.


They are declarations of objects or functions
defined elsewhere with external linkage. (IMHO they
are also stylistically repugnant, but that's another
discussion.)


I've seen things like this too:

{
extern int foo = 10;

/* rest of block */
}

So if extern means it's a declaration, would that line really mean:

{
extern in foo; /* declare an already defined object */
foo = 10; /* assign to that object */

/* rest of block */
}

Nov 14 '05 #5


G Patel wrote:

I've seen things like this too:

{
extern int foo = 10;

/* rest of block */
}


If you've seen them and the compiler didn't complain,
the compiler was being operated in a non-conforming mode:

6.7.8 Initialization
/5/ If the declaration of an identifier has block
scope, and the identifier has external or internal
linkage, the declaration shall have no initializer
for the identifier.

If you've seen `extern int foo = 10;' at file scope,
outside a block, that's fine: it's a definition of `foo'
with an initializer, specifying (unnecessarily) external
linkage. But it's not permitted to do this inside a block.

--
Er*********@sun .com

Nov 14 '05 #6
Eric Sosman wrote:
G Patel wrote:

I've seen things like this too:

{
extern int foo = 10;

/* rest of block */
}


If you've seen them and the compiler didn't complain,
the compiler was being operated in a non-conforming mode:

6.7.8 Initialization
/5/ If the declaration of an identifier has block
scope, and the identifier has external or internal
linkage, the declaration shall have no initializer
for the identifier.

If you've seen `extern int foo = 10;' at file scope,
outside a block, that's fine: it's a definition of `foo'
with an initializer, specifying (unnecessarily) external
linkage. But it's not permitted to do this inside a block.


You're right, I've only seen extern with initializer at the top of a
file. This is weird, so with an intializer it's a definition, without
an initializer it's a declaraction.

I have one more question. In multi source file programs, I put extern
declaractions for variables I want to share amongst many source files
in a headder, and include that in all those source files. I never used
to include that in the file I defined the file-scope variable. I
tried it after a suggestion and it works. But is this 100% proper (not
undefined)?

Technically, after preprocessing that source file has this at the top:
extern T var; /* came in from #include */

T var; /* was in the .c file */

I know the behaviour when extern declaration follows a non-extern
declaraction is okay, because the extern takes the
linkage/characteristics of the non-extern declaration (either static or
implicity external). But how does this work when extern declaration
PRECEDES a non-extern declaration?

Am I safe to include the header like this?

Nov 14 '05 #7

G Patel wrote:

--snipped--

I know the behaviour when extern declaration follows a non-extern
declaraction is okay, because the extern takes the
linkage/characteristics of the non-extern declaration (either static or implicity external). But how does this work when extern declaration
PRECEDES a non-extern declaration?

Am I safe to include the header like this?


You need to get yourself a copy of K&R2.

Nov 14 '05 #8
G Patel wrote:

I have one more question. In multi source file programs, I put extern declaractions for variables I want to share amongst many source files
in a headder, and include that in all those source files. I never used to include that in the file I defined the file-scope variable. I
tried it after a suggestion and it works. But is this 100% proper (not undefined)?

No. This is perfectly fine to do. This is actually more convenient
(add the header in every source file that requires the use of the
identifier) and allows the compiler to better catch descrepancies
between the single definition and the form of the declarations.

Technically, after preprocessing that source file has this at the top:

extern T var; /* came in from #include */

T var; /* was in the .c file */

I know the behaviour when extern declaration follows a non-extern
declaraction is okay, because the extern takes the
linkage/characteristics of the non-extern declaration (either static or implicity external).

Yes. The standard says...

6.2.2 Linkage of Identifiers
+++++++++++++++ ++++++++++++++
(4) For an identifier with the storage-class specifier extern in a
scope in which a prior declaration of that identifier is visible [and
has internal or external linkage] ... the linkage of that later
declaration is the same ... (continued below)
So this explains a situation such as:

T var; /* has external linkage */
extern T var; /* has same linkage - external */

OR

static T var; /* has internal linkage */
extern T var; /* has same linkage - internal */

Note: "NO LINKAGE" doesn't have the same affect on later extern
declarations.

You said you already knew this, which is good because it will help
understand what happens when the order is switched.

But how does this work when extern declaration
PRECEDES a non-extern declaration?


The standard says...

6.2.2 Linkage of Identifiers
+++++++++++++++ ++++++++++++++
(4 continued from above)... If no prior declaration is visible [for
declaration with extern storage-class specifier], or if the prior
declaration specifies no linkage, then the identifier has external
linkage.

(5) ... If the declaration of an identifier for an object has file
scope and no storage-class specifier, its linkage is external.
This gives you the answer to your question. The first sentence tells
us that extern declaraed identifiers have external linkage if there is
no previous identical identifier with external/internal linkage. The
second sentence tells us that a declaration at file scope without a
storage class specifier has external linkage.

So in your question:

/* external linkage - no previous internal/external linkage */
extern T var;

T var; /* external linkage - no storage class specifier & file scope */
So both become declaration with external linkage, and specify the same
objects (linkage works when identifiers are in the same scope , or
different scopes.)

Nov 14 '05 #9

Beta What wrote:
G Patel wrote:

--snipped--

I know the behaviour when extern declaration follows a non-extern
declaraction is okay, because the extern takes the
linkage/characteristics of the non-extern declaration (either static or
implicity external). But how does this work when extern

declaration PRECEDES a non-extern declaration?

Am I safe to include the header like this?


You need to get yourself a copy of K&R2.


K&R2 wouldn't help the OP much in this case (appendix *might* provide
*some* help).

Nov 14 '05 #10

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

Similar topics

4
4255
by: Sergei | last post by:
I ran into this problem. I needed to create an entry for access to a library of functions that are "extern C", and I just can't find the right syntax, if it exists at all ( I am using MSVC 6.0-7.1).. Note, that basically I don't need an "extern C" linkage, I just need to define a type to cast a resolved function address.. Any ideas? Basically , I need something like this..
1
2648
by: terrencel | last post by:
I was told to look at some old C code that was ported to C++. One of the file is like: ========================================= CPPClass* someCPPVar = NULL; extern "C" {
3
3002
by: Andy | last post by:
Hi, This is just a simple hello world program. #include <iostream> using namespace std; int main() { cout<<"Hello,world\n"; }
4
2763
by: cpptutor2000 | last post by:
Could some C++ guru help me please? I am trying to build an application using gcc 3.2.3, that has a some classes using functions defined in some C files in the same directory. If inside the C++ source file I use: extern "C"{ // C function declarations } //C++ code
1
1461
by: Phlip | last post by:
Language Lawyers: Compare this: extern "C" int Maine() { ... bool runTests(); return !runTests(); }
19
3854
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
3777
by: William Pursell | last post by:
I debated whether this belongs in comp.lang.c++, and finally decided that it belongs here, because it really is a C question... Suppose I have a perfectly friendly, harmless library, libfoo, that presents a single function declared: int foo_function(int x); The world is simple and all the small creatures rejoice in its simplicity.
3
1820
by: parag_paul | last post by:
Does the following typedef differ from a typedef inside a extern "C" #if defined(__cplusplus) extern "C" { #endif typedef int (*fun_point)(int);
5
2925
by: Medvedev | last post by:
What's that preprocessor do #ifdef __cplusplus extern "C" { #endif .. .. .. #ifdef __cplusplus } #endif
0
8672
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9257
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
9123
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
9017
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,...
1
6594
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
5923
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
4434
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
3125
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
2
2462
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.