Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Use of extern

1 view
Skip to first unread message

john...@my-deja.com

unread,
Apr 25, 2000, 3:00:00 AM4/25/00
to
Greetings,

I have a question about the use of the "extern" keyword. I've been
reading stuff about it over and over again in my friend's book, C: A
Reference Manual, but I am still not clear about certain aspects of it.

Here's an example I can think of when using extern:

-- cookie.c --
#include <stdio.h>

int beep;

int main(void)
{
extern int beep;
beep = 5;
printf("%d\n", beep);

return 0;
}
-- /cookie.c --

In the above example, extern is not necessary. I could use beep without
it.

The only application I can think of where extern is truly necessary, is
this: I have 2 source files. I have a global variable (outside of any
block) in one of them, and I want to use it in the other. I do the
following:

-- c1.c --
int duck = 5;
-- /c1.c --

-- c2.c --
extern int duck;
duck = 10;
-- /c2.c --

a] is that the only application for extern?
b] if the variable `duck' in c1.c would be inside a function, would
there be a way to import it to c2.c?

Oh, and final question: someone told me that extern declarations should
*ALWAYS* be inside header files. In the example I gave above, with c1.c
and c2.c, I could use a header file as follows:

-- c1.c --
int duck = 10;
-- /c1.c --

-- c2.c --
#include "bazzy.h"
duck = 50;
-- /c2.c --

-- h1.h --
extern int duck;
-- /h1.h --

The only advantage to this example with a header is that I could include
h1.h in a source file other than c2.c and use `duck' from there too, but
that seems to incourage uses of global variables.. so why the rule that
good programming should use extern declarations only in headers?

Thanks,
-- John

Joke O' day:
char acter;


Sent via Deja.com http://www.deja.com/
Before you buy.

Jack Klein

unread,
Apr 25, 2000, 3:00:00 AM4/25/00
to
On Tue, 25 Apr 2000 03:03:42 GMT, john...@my-deja.com wrote in
comp.lang.c:

You are confused about a few things here.

This second statement could only be inside of a function.

> -- /c2.c --
>
> a] is that the only application for extern?
> b] if the variable `duck' in c1.c would be inside a function, would
> there be a way to import it to c2.c?

No, not directly. You could pass a pointer to it to a function in
c2.c, which could then change the value.

> Oh, and final question: someone told me that extern declarations should
> *ALWAYS* be inside header files. In the example I gave above, with c1.c
> and c2.c, I could use a header file as follows:
>
> -- c1.c --
> int duck = 10;
> -- /c1.c --
>
> -- c2.c --
> #include "bazzy.h"
> duck = 50;

Again this assignment statement could only be inside a function.

> -- /c2.c --
>
> -- h1.h --
> extern int duck;
> -- /h1.h --
>
> The only advantage to this example with a header is that I could include
> h1.h in a source file other than c2.c and use `duck' from there too, but
> that seems to incourage uses of global variables.. so why the rule that
> good programming should use extern declarations only in headers?
>
> Thanks,
> -- John

You are wrong about that being the only advantage. Consider the fact
that you originally define a global variable of type float. You
define and initialize the float in one file, declare it as extern in
another.

Later you realize that you need to change it to a double because you
need more digits of precision. You change it in the file where you
define it, but forget to change it in the second file where it is
declared as extern. Maybe your linker catches this and warns you,
maybe your program crashes and burns when you run it.

If you put the extern declaration in a header you include the header
in the source file that defines the external object as well as in all
other source files that will reference it.

If you update the defining source file without updating the header,
you will get an error compiling the source because you will have two
different types for the same object. Once you update the header all
of the other sources that refer to the object will be updated as well.

Never keep the same information in more than one file is you can put
it in one place only and let the compiler make sure changes are
handled properly everywhere.

Jack Klein
--
Home: http://jackklein.home.att.net

Gilford Wimbley

unread,
Apr 25, 2000, 3:00:00 AM4/25/00
to
On Tue, 25 Apr 2000 03:03:42 GMT, john...@my-deja.com wrote:

>Greetings,
>
>I have a question about the use of the "extern" keyword. I've been
>reading stuff about it over and over again in my friend's book, C: A
>Reference Manual, but I am still not clear about certain aspects of it.
>

[snip]


>The only application I can think of where extern is truly necessary, is
>this: I have 2 source files. I have a global variable (outside of any
>block) in one of them, and I want to use it in the other. I do the
>following:
>
>-- c1.c --
>int duck = 5;
>-- /c1.c --
>
>-- c2.c --
>extern int duck;
>duck = 10;

>-- /c2.c --
>
>a] is that the only application for extern?
>b] if the variable `duck' in c1.c would be inside a function, would
>there be a way to import it to c2.c?

No, I don't think so. In fact, when a variable is declared locally
inside of a function (i.e., its storage class is neither static nor
extern), it doesn't really exist outside of the function.


>
>Oh, and final question: someone told me that extern declarations should
>*ALWAYS* be inside header files. In the example I gave above, with c1.c
>and c2.c, I could use a header file as follows:
>

[snip]


>The only advantage to this example with a header is that I could include
>h1.h in a source file other than c2.c and use `duck' from there too, but
>that seems to incourage uses of global variables.. so why the rule that
>good programming should use extern declarations only in headers?
>

I think the reason to put it in a header is that doing so guarantees
that it is declared exactly once, and that any program that wants to
use it only has to include the header. Its just centralization,
really. Associate the variable with the functions that use it.

>Thanks,
> -- John
>
>Joke O' day:
>char acter;
>
>
>Sent via Deja.com http://www.deja.com/
>Before you buy.

I'll give you one example where I used extern variables in the past. I
was writing code that interfaced to a pre-existing library. As part
of this interfacing, I had to write a "clean-up" function (called by
the library and taking no arguments) which could basically be called
any time. Since events beyond my control could lead to the clean-up
code being called even before my program reached a normal termination,
I had to free memory in the clean-up code. Hence, I had to get a
variable in there without passing it in. That is why I used the
extern specifier.

Also, there is "errno", an external global declared by part of the c
standard library. But all in all, I think you are correct to view
extern as something to be avoided when possible.

regards,
GW

0 new messages