473,805 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to use the keyword extern in c?

hi all ,
anyone could tell me the useage of the keyworkd "extern" in c? and
the difference between c & cplusplus?

in the C99Rationalv5.1 0.pdf it lists 4 cases. while the first
common cases will incur the vc compiler to complain.
Nov 14 '05 #1
16 1821
On 24 Aug 2004 19:15:30 -0700, fj***@163.net (ooze) wrote in
comp.lang.c:
hi all ,
anyone could tell me the useage of the keyworkd "extern" in c?
The extern keyword specifies that the identifier being declared has
external linkage. This allows an object or function to be defined in
one translation unit (basically, source file and everything it
includes) and referred to by name from code in other translation
units.
and
the difference between c & cplusplus?
C++ is off-topic here, news:comp.lang. c++ is down the hall to the
right.
in the C99Rationalv5.1 0.pdf it lists 4 cases. while the first
common cases will incur the vc compiler to complain.


As to what Visual C++ might complain about, it might make a difference
whether you are using it as a C or C++ compiler, as it handles both
languages.

The document you reference is not necessarily one that many people
have.

Even for those who do, like I do, your reference is far too vague.
The document is well over 200 pages, and the word 'extern' appears
many times. I spent a brief time looking in both the PDF and printed
versions I have, and did not find the section you are referring to.

Post again and include the specific section number from the document.
That is, if you are indeed compiling C code and not C++. And for
readers here who do not have a copy of the rationale, copy and paste
the code sample you are referring to, and copy and paste the compiler
or linker error messages as well.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2
Jack Klein <ja*******@spam cop.net> wrote in message news:<ti******* *************** **********@4ax. com>...
On 24 Aug 2004 19:15:30 -0700, fj***@163.net (ooze) wrote in
comp.lang.c:
hi all ,
anyone could tell me the useage of the keyworkd "extern" in c?


The extern keyword specifies that the identifier being declared has
external linkage. This allows an object or function to be defined in
one translation unit (basically, source file and everything it
includes) and referred to by name from code in other translation
units.
and
the difference between c & cplusplus?


C++ is off-topic here, news:comp.lang. c++ is down the hall to the
right.
in the C99Rationalv5.1 0.pdf it lists 4 cases. while the first
common cases will incur the vc compiler to complain.


As to what Visual C++ might complain about, it might make a difference
whether you are using it as a C or C++ compiler, as it handles both
languages.

The document you reference is not necessarily one that many people
have.

Even for those who do, like I do, your reference is far too vague.
The document is well over 200 pages, and the word 'extern' appears
many times. I spent a brief time looking in both the PDF and printed
versions I have, and did not find the section you are referring to.

Post again and include the specific section number from the document.
That is, if you are indeed compiling C code and not C++. And for
readers here who do not have a copy of the rationale, copy and paste
the code sample you are referring to, and copy and paste the compiler
or linker error messages as well.

hallo,

in section 6.2.2 Linkages of identifiers
there is a table listed the 4 cases also includes detail info.

what's the difference when the keyword "extern" appears and when it doest appear?
for instance,
+++++++++++++++ +++++++++++++++ +
extern int var1; | int var1;
_______________ _______________ _
extern void func(); | void func();

in a c file.
Nov 14 '05 #3
Jack Klein <ja*******@spam cop.net> wrote in message news:<ti******* *************** **********@4ax. com>...
On 24 Aug 2004 19:15:30 -0700, fj***@163.net (ooze) wrote in
comp.lang.c:
hi all ,
anyone could tell me the useage of the keyworkd "extern" in c?


The extern keyword specifies that the identifier being declared has
external linkage. This allows an object or function to be defined in
one translation unit (basically, source file and everything it
includes) and referred to by name from code in other translation
units.
and
the difference between c & cplusplus?


C++ is off-topic here, news:comp.lang. c++ is down the hall to the
right.
in the C99Rationalv5.1 0.pdf it lists 4 cases. while the first
common cases will incur the vc compiler to complain.


As to what Visual C++ might complain about, it might make a difference
whether you are using it as a C or C++ compiler, as it handles both
languages.

The document you reference is not necessarily one that many people
have.

Even for those who do, like I do, your reference is far too vague.
The document is well over 200 pages, and the word 'extern' appears
many times. I spent a brief time looking in both the PDF and printed
versions I have, and did not find the section you are referring to.

Post again and include the specific section number from the document.
That is, if you are indeed compiling C code and not C++. And for
readers here who do not have a copy of the rationale, copy and paste
the code sample you are referring to, and copy and paste the compiler
or linker error messages as well.


hallo,

in section 6.2.2 Linkages of identifiers
there is a table listed the 4 cases also includes detail info.

what's the difference when the keyword "extern" appears and when it doest appear?
for instance,
+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ +
extern int var1; | int var1;
_______________ _______________ _______________ _______________ _
extern int var1 = 0x0bad | int var1 = 0x0bad
_______________ _______________ _______________ _______________ _
extern void func(); | void func();
+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++

in a c file.
Nov 14 '05 #4
In <18************ **************@ posting.google. com> fj***@163.net (ooze) writes:
what's the difference when the keyword "extern" appears and when it doest appear?
for instance,
Assuming all declarations have file scope:
++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++
extern int var1; | int var1;
declaration, external linkage tentative external definition
______________ _______________ _______________ _______________ __
extern int var1 = 0x0bad; | int var1 = 0x0bad;
external definition external definition
______________ _______________ _______________ _______________ __
extern void func(); | void func();


declaration, external linkage declaration, external linkage

As you can see, the only context where extern makes any difference is
in object declarations that are not intended to be definitions. Otherwise
it can be omitted in file scope declarations, because it is the default.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5
Da*****@cern.ch (Dan Pop) wrote in message news:<cg******* ***@sunnews.cer n.ch>...
In <18************ **************@ posting.google. com> fj***@163.net (ooze) writes:
what's the difference when the keyword "extern" appears and when it doest appear?
for instance,


Assuming all declarations have file scope:
++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++
extern int var1; | int var1;


declaration, external linkage tentative external definition
______________ _______________ _______________ _______________ __
extern int var1 = 0x0bad; | int var1 = 0x0bad;


external definition external definition
______________ _______________ _______________ _______________ __
extern void func(); | void func();


declaration, external linkage declaration, external linkage

As you can see, the only context where extern makes any difference is
in object declarations that are not intended to be definitions. Otherwise
it can be omitted in file scope declarations, because it is the default.

Dan


thanks, as you stated,
I remember in C the defintion of an object can ONLY occur ONLY ONCE.

but if I write extern int var1 = 0x0bad ; in one.c,
and write extern int var1 = 0xdead; in another.c
the vc c compiler will happy accept it.

since you think
______________ _______________ _______________ _______________ __
extern int var1 = 0x0bad; | int var1 = 0x0bad;


external definition external definition


how to explain it?

thanks
Nov 14 '05 #6
On 25 Aug 2004 18:37:26 -0700, fj***@163.net (ooze) wrote in
comp.lang.c:
Da*****@cern.ch (Dan Pop) wrote in message news:<cg******* ***@sunnews.cer n.ch>...
In <18************ **************@ posting.google. com> fj***@163.net (ooze) writes:
what's the difference when the keyword "extern" appears and when it doest appear?
for instance,


Assuming all declarations have file scope:
++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++
extern int var1; | int var1;


declaration, external linkage tentative external definition
______________ _______________ _______________ _______________ __
extern int var1 = 0x0bad; | int var1 = 0x0bad;


external definition external definition
______________ _______________ _______________ _______________ __
extern void func(); | void func();


declaration, external linkage declaration, external linkage

As you can see, the only context where extern makes any difference is
in object declarations that are not intended to be definitions. Otherwise
it can be omitted in file scope declarations, because it is the default.

Dan


thanks, as you stated,
I remember in C the defintion of an object can ONLY occur ONLY ONCE.

but if I write extern int var1 = 0x0bad ; in one.c,
and write extern int var1 = 0xdead; in another.c
the vc c compiler will happy accept it.

since you think
______________ _______________ _______________ _______________ __
extern int var1 = 0x0bad; | int var1 = 0x0bad;


external definition external definition


how to explain it?

thanks


There are a lot of technical details about different models of linking
that is used by the linkers of different implementations on different
platforms. Some of them will allow multiple definitions of an
external symbol, as long as no more than one of them has an
initializer.

The C standard requires that any object or function with external
linkage that is not actually referenced in the program must have
exactly 0 or 1 definitions somewhere in the program. Any object
or function with external linkage that is actually referenced in a
program must have exactly 1 definition, no more and no less.

A violation of this requirement causes undefined behavior, and
compilers are never required to diagnose undefined behavior.

Any compiler should be able to compile the two source files with two
external definitions of 'var1', both with initializers. The error
should come when the two object files are linked together to make the
program.

I don't know of any version of Microsoft's Visual C++, or any other C
compiler for that matter, that will not generate an error if those two
source files are put into a single program.

Here is what I got from Visual C++ 6.0:

--------------------Configuration: multi - Win32
Debug--------------------
Linking...
multi2.obj : error LNK2005: _var1 already defined in multi1.obj
multi___Win32_D ebug/multi.exe : fatal error LNK1169: one or more
multiply defined symbols found
Error executing link.exe.

multi.exe - 2 error(s), 0 warning(s)

And here is what I got from the Visual C++ 2005 Express Edition Beta:

------ Build started: Project: multi, Configuration: Debug Win32
------
Compiling...
multi2.c
Linking...
multi1.obj : error LNK2005: _var1 already defined in multi2.obj
Debug/multi.exe : fatal error LNK1169: one or more multiply defined
symbols found
Build log was saved at "file://c:\Program Files\Microsoft Visual
Studio 8\projects\mult i\multi\Debug\B uildLog.htm"
multi - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #7
Jack Klein <ja*******@spam cop.net> wrote in message news:<fh******* *************** **********@4ax. com>...
On 25 Aug 2004 18:37:26 -0700, fj***@163.net (ooze) wrote in
comp.lang.c:
Da*****@cern.ch (Dan Pop) wrote in message news:<cg******* ***@sunnews.cer n.ch>...
In <18************ **************@ posting.google. com> fj***@163.net (ooze) writes:

>what's the difference when the keyword "extern" appears and when it doest appear?
>for instance,

Assuming all declarations have file scope:

>++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++
> extern int var1; | int var1;

declaration, external linkage tentative external definition

>______________ _______________ _______________ _______________ __
>extern int var1 = 0x0bad; | int var1 = 0x0bad;

external definition external definition

>______________ _______________ _______________ _______________ __
>extern void func(); | void func();

declaration, external linkage declaration, external linkage

As you can see, the only context where extern makes any difference is
in object declarations that are not intended to be definitions. Otherwise
it can be omitted in file scope declarations, because it is the default.

Dan


thanks, as you stated,
I remember in C the defintion of an object can ONLY occur ONLY ONCE.

but if I write extern int var1 = 0x0bad ; in one.c,
and write extern int var1 = 0xdead; in another.c
the vc c compiler will happy accept it.

since you think
>______________ _______________ _______________ _______________ __
>extern int var1 = 0x0bad; | int var1 = 0x0bad;

external definition external definition


how to explain it?

thanks


There are a lot of technical details about different models of linking
that is used by the linkers of different implementations on different
platforms. Some of them will allow multiple definitions of an
external symbol, as long as no more than one of them has an
initializer.

The C standard requires that any object or function with external
linkage that is not actually referenced in the program must have
exactly 0 or 1 definitions somewhere in the program. Any object
or function with external linkage that is actually referenced in a
program must have exactly 1 definition, no more and no less.

A violation of this requirement causes undefined behavior, and
compilers are never required to diagnose undefined behavior.

Any compiler should be able to compile the two source files with two
external definitions of 'var1', both with initializers. The error
should come when the two object files are linked together to make the
program.

I don't know of any version of Microsoft's Visual C++, or any other C
compiler for that matter, that will not generate an error if those two
source files are put into a single program.

Here is what I got from Visual C++ 6.0:

--------------------Configuration: multi - Win32
Debug--------------------
Linking...
multi2.obj : error LNK2005: _var1 already defined in multi1.obj
multi___Win32_D ebug/multi.exe : fatal error LNK1169: one or more
multiply defined symbols found
Error executing link.exe.

multi.exe - 2 error(s), 0 warning(s)

And here is what I got from the Visual C++ 2005 Express Edition Beta:

------ Build started: Project: multi, Configuration: Debug Win32
------
Compiling...
multi2.c
Linking...
multi1.obj : error LNK2005: _var1 already defined in multi2.obj
Debug/multi.exe : fatal error LNK1169: one or more multiply defined
symbols found
Build log was saved at "file://c:\Program Files\Microsoft Visual
Studio 8\projects\mult i\multi\Debug\B uildLog.htm"
multi - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========

what's the difference between
"tentative external definition" &

"external definition"?

vc will accept if one object defined twice with the above respectivly.
Nov 14 '05 #8
Jack Klein <ja*******@spam cop.net> wrote in message news:<fh******* *************** **********@4ax. com>...
On 25 Aug 2004 18:37:26 -0700, fj***@163.net (ooze) wrote in
comp.lang.c:
Da*****@cern.ch (Dan Pop) wrote in message news:<cg******* ***@sunnews.cer n.ch>...
In <18************ **************@ posting.google. com> fj***@163.net (ooze) writes:

>what's the difference when the keyword "extern" appears and when it doest appear?
>for instance,

Assuming all declarations have file scope:

>++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++
> extern int var1; | int var1;

declaration, external linkage tentative external definition

>______________ _______________ _______________ _______________ __
>extern int var1 = 0x0bad; | int var1 = 0x0bad;

external definition external definition

>______________ _______________ _______________ _______________ __
>extern void func(); | void func();

declaration, external linkage declaration, external linkage

As you can see, the only context where extern makes any difference is
in object declarations that are not intended to be definitions. Otherwise
it can be omitted in file scope declarations, because it is the default.

Dan


thanks, as you stated,
I remember in C the defintion of an object can ONLY occur ONLY ONCE.

but if I write extern int var1 = 0x0bad ; in one.c,
and write extern int var1 = 0xdead; in another.c
the vc c compiler will happy accept it.

since you think
>______________ _______________ _______________ _______________ __
>extern int var1 = 0x0bad; | int var1 = 0x0bad;

external definition external definition


how to explain it?

thanks


There are a lot of technical details about different models of linking
that is used by the linkers of different implementations on different
platforms. Some of them will allow multiple definitions of an
external symbol, as long as no more than one of them has an
initializer.

The C standard requires that any object or function with external
linkage that is not actually referenced in the program must have
exactly 0 or 1 definitions somewhere in the program. Any object
or function with external linkage that is actually referenced in a
program must have exactly 1 definition, no more and no less.

A violation of this requirement causes undefined behavior, and
compilers are never required to diagnose undefined behavior.

Any compiler should be able to compile the two source files with two
external definitions of 'var1', both with initializers. The error
should come when the two object files are linked together to make the
program.

I don't know of any version of Microsoft's Visual C++, or any other C
compiler for that matter, that will not generate an error if those two
source files are put into a single program.

Here is what I got from Visual C++ 6.0:

--------------------Configuration: multi - Win32
Debug--------------------
Linking...
multi2.obj : error LNK2005: _var1 already defined in multi1.obj
multi___Win32_D ebug/multi.exe : fatal error LNK1169: one or more
multiply defined symbols found
Error executing link.exe.

multi.exe - 2 error(s), 0 warning(s)

And here is what I got from the Visual C++ 2005 Express Edition Beta:

------ Build started: Project: multi, Configuration: Debug Win32
------
Compiling...
multi2.c
Linking...
multi1.obj : error LNK2005: _var1 already defined in multi2.obj
Debug/multi.exe : fatal error LNK1169: one or more multiply defined
symbols found
Build log was saved at "file://c:\Program Files\Microsoft Visual
Studio 8\projects\mult i\multi\Debug\B uildLog.htm"
multi - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========

if int var1; defined in one.c, and extern int var1=23; defined in
another.c.
vc will accept it.
vc will also accept
1) int var1 in one.c and int var1 in another.c
2) int var1 in one.c and int var1 = 23 in another.c

it seems to me that in a program an object can have mutiple tentative
definitions, but only one external definition.
Nov 14 '05 #9
In <18************ **************@ posting.google. com> fj***@163.net (ooze) writes:
if int var1; defined in one.c, and extern int var1=23; defined in
another.c.
vc will accept it.
vc will also accept
1) int var1 in one.c and int var1 in another.c
2) int var1 in one.c and int var1 = 23 in another.c
Assuming all declarations have file scope, there are two external
definitions for var1 in all your examples.
it seems to me that in a program an object can have mutiple tentative
definitions, but only one external definition.


Only fools try to figure out how the language works from the behaviour of
their compilers. If multiple definitions of an object cause undefined
behaviour, any implementation is free to silently accept them. And,
according to Annex J.2 (I'm too lazy to search the chapter and verse):

- An identifier with external linkage is used, but in the program
there does not exist exactly one external definition for the
identifier, or the identifier is not used and there exist
multiple external definitions for the identifier (6.9).

So, VC is free to silently accept as many external definitions for var1
as you provide. But the behaviour of your program is undefined, if it
provides more than one.

A tentative definition becomes a genuine definition at the end of the
translation unit, if not superseded by another definition for the same
object. This is why it is called "tentative" definition. So, your
conclusion is valid only for one translation unit (C source file plus
whatever stuff is included via #include directives), not for a complete
program containing multiple definitions for the same object in multiple
translation units.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #10

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

Similar topics

20
3158
by: Grumble | last post by:
Hello everyone, As far as I understand, the 'inline' keyword is a hint for the compiler to consider the function in question as a candidate for inlining, yes? What happens when a function with extern linkage is inlined? Should the compiler still export the function? Or is an inlined function implicitly static?
9
6370
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static keyword is useful inside struct, class, and function only unless you want to force local variable to be global variable so static is used. Do you have idea why most programmers do this? Bryan Parkoff
18
4293
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.
5
16611
by: siliconwafer | last post by:
Hi all, I wanted to know that is use of extern keyword mandatory in case of global variables and functions used in other source files? i.e consider a following piece of code from MSDN explaining extern storage class: /****************************************************************** SOURCE FILE ONE *******************************************************************/ extern int i; /* Reference to i, defined below */
32
3057
by: lcdgoncalves | last post by:
Hi everyone Is there a real need to use keyword static with functions, if we simply don't declare their prototypes in .h file? Many textbooks avoid to discuss this matter and/or discuss only the usage of static functions. I've been programming for years and I never felt a real need for the "static approach for functions". I don't know if this is a trivial matter since the reserved word
0
1097
by: SamLee | last post by:
Hi, how can I use extern keyword for example. EXEC SQL BEGIN DECLARE SECTION; extern int sal; EXEC SQL END DECLARE SECTION;
0
9716
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
9596
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
10607
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...
1
10364
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
10104
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...
0
6875
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
5541
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
4317
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
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.