The extern keyword. Most C programmers
have seen it, but many don’t understand what it is and what it actually means
when it shows up in some code that you’re reading or code that you’re
writing. It’s really easy for extern to fly under the radar for students because
you can often add it or remove it from programs and not see any change in the
behavior. So, today I want to help you understand what this keyword is actually
meaning. So, C programs are built from one or more translation units—or compilation
units they’re sometimes called. Let’s say I have a bunch of C files and some
header files. I’m gonna compile these together. Your make file probably does
something like this, where each of your C files is compiled into an object file
with a “.o” extension. Each of these object files, or the C file that goes
with it—each of these object files is a separate translation unit. They’re
compiled individually, and in the final step they’re all going to be linked
together into a single executable binary. Now, let’s say that we wanted to declare
a variable in one translation unit but then use it in another one. That’s where
extern comes in. When I declare a variable as extern, it tells the compiler
that this variable exists, and I’m going to use it but it’s actually declared
somewhere else—in another translation unit. When the object files are linked
together, this results in a single variable that can be used throughout the
program. But, is extra necessary? What happens if I leave it off? Well, often
nothing happens. In the old days, this would give you an error because it would
see two variables in two different translation units with the same name.
Modern linkers see this and generally assume that you meant to have just one
variable that’s shared across your whole program. So, they assume extern. So, if they
assume extern, then why use it? Well, if I add an initializer for this variable, I’m
gonna get an error. Having two initializers for the same variable
doesn’t work. And, the error is a duplicate symbol error, which sometimes
confuses students. If I add extern then you get two benefits. I still get an
error but the error is more helpful. It says, “Hey Jacob.
You just initialized an extern. Bad form, my friend.” And, I think this error
message is actually more readable, which is really nice.
The second benefit is that extern declarations make your code
easier for others to read. It tells them that the variable is defined somewhere
else, which can then help prevent confusion as they edit the code. Now as a
final comment, just because now you understand what extern means, and you know
that that allows you to share global state across different translation units,
doesn’t mean you should go run off and use extern everywhere and use a bunch of
global variables. Sharing global state in a program is dangerous. It’s risky. It
adds all sorts of problems with readability and maintainability. And, so
be careful. Just because you understand extern, doesn’t mean you should always
use it. In most cases, local variables are still your most safe and sane option, and
they’re usually recommended. But, now at least you know what extern means, and you
know how to use it to get better error messages and to communicate with other
programmers and make your code more readable. And, that’s all I have for you
today until next time happy coding and have a great day

Categories: ArticlesBlog

15 Comments

Julian Sp · January 26, 2018 at 11:35 pm

I don't know how, but I wish that you'd have a greater following. Explaining C is not a trivial thing. Keep it up!

undercurrent · February 14, 2018 at 4:16 pm

thanks i was watching your testing video and saw the keyword extern. And remembering the other video you made about trying to understand code instead of just accepting it I wanted to know what extern meant.

sidekick rida · May 26, 2018 at 3:51 pm

extern can also be used for functions, not just variables

Xingmin Zhang · June 6, 2018 at 2:54 am

excellent

Arthur Vieira · June 19, 2018 at 2:29 pm

I have to point out… The 'N' at 0:06 is upside down! xD

Mimo N · July 11, 2018 at 7:06 pm

Finally … a good explanation! 😀

Shihab Hossain Auni · July 16, 2018 at 11:34 am

What is that music in the background?

LPFan MusicHouse · September 20, 2018 at 2:02 am

I have a question, what if i had 3 files, file1.c and file2.c and file3.c,
what if i defined a global variable in file1, and used extern decleration in file2, and tried to do something like : x=10 in file3 without even using extern decleration in file3, will it give an error as extern extends the variable to be seen in that file in which extern was used only, or extern in any file extends the scope throughout all files even if i used extern in only one file (which wouldn't give an error for my example)?

science blossom · December 3, 2018 at 10:03 pm

perfect

Kurt Gazes · January 17, 2019 at 10:17 pm

Please make a video about “inline”. How do you check if the compiler actually inclined a function? And what is the correct way to inline a function defined in a different translation unit?

Hamza Iqbal · April 25, 2019 at 7:10 pm

That's what I was looking for… Thanks

Vatsal Mehta · May 31, 2019 at 9:32 am

I had my phone with full volume level and with headphones ON!

RIP my Ears!

james pong · July 4, 2019 at 5:10 pm

I've worked on a bunch of large scale programs over the years,

At first we tried to use best practices oop inheritance and pass references when needed

As I matured I came to the realization that it is much more easy to maintain and avoid bugs by using extern in many cases.

I combine c and c++ style in my code, avoid inheritance except when it really makes sense,

I hide complexity and make sure code separates into logical sections

So that it's readable

Aslan M · August 5, 2019 at 10:25 am

Hi Jacob! Thank you for explanation.
PS: It would be nice to reduce the sound of music in the background because it interferes the concentration.

mehdi attia · October 9, 2019 at 10:13 pm

which editor are you using

Leave a Reply

Your email address will not be published. Required fields are marked *