473,320 Members | 1,868 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" 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 2684

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
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote:
#include <stdio.h>

int main(int argc, char* argv[]) {
extern void* malloc(size_t);
This loses (use the headers, damn it!)...
extern void free(void*);
....this loses...
const size_t n = 13;
int* p = (int*)malloc(n*sizeof(int));
....this loses majorly (and you know it)...
free((void*)p);
....and now you're just being silly.
return 0;
}

When, for example, I want to use a private helper function.


There is no such thing in C.

Richard
Nov 14 '05 #11
On 2005-02-23 09:56, E. Robert Tisdale wrote:
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.


You mean 'extremely rarely' or 'never', of course.
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.


All that because it is easier to type two extra lines with 'prototypes'
for malloc() and free(), just for the fun of making your life difficult?

You could have just included <stdlib.h> you know :P

Nov 14 '05 #12
Giorgos Keramidas wrote:
On 2005-02-23 09:56, E. Robert Tisdale wrote:
.... snip ...
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.


All that because it is easier to type two extra lines with 'prototypes'
for malloc() and free(), just for the fun of making your life difficult?

You could have just included <stdlib.h> you know :P

_____________________
/| /| | |
||__|| | Please do not |
/ O O\__ | feed the |
/ \ | Trolls |
/ \ \|_____________________|
/ _ \ \ ||
/ |\____\ \ ||
/ | | | |\____/ ||
/ \|_|_|/ | _||
/ / \ |____| ||
/ | | | --|
| | | |____ --|
* _ | |_|_|_| | \-/
-- _--\ _ \ | ||
/ _ \\ | / `
/ \_ /- | | |
* ___ c_c_c_C/ \C_c_c_c____________

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #13

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

Similar topics

4
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...
1
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
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
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++...
1
by: Phlip | last post by:
Language Lawyers: Compare this: extern "C" int Maine() { ... bool runTests(); return !runTests(); }
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: 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,...
3
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
by: Medvedev | last post by:
What's that preprocessor do #ifdef __cplusplus extern "C" { #endif .. .. .. #ifdef __cplusplus } #endif
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
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.