Understanding the Extern Keyword in C

preview_player
Показать описание
---

Understanding the Extern Keyword in C // Why does this variable have "extern" in front of it? Each semester a lot of students ask me about extern variables in C. Here's a first stab at an answer.



***

Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.

About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.

More about me and what I do:

To Support the Channel:
+ like, subscribe, spread the word

Want me to review your code?

You can also find more info about code reviews here.
Рекомендации по теме
Комментарии
Автор

I love you man! A single video of yours beats reading out hundreds of unstructured forum pages trying to explain a concept. The moment I saw you had a video explaining extern I had a big grin on my face as I knew a big chunk of my time is about to be saved. Thanks a bunch!

EvilSapphireR
Автор

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!

JNeverMindMe
Автор

I’m really enjoying your channel. As a former embedded FW developer (I’ve since gone to the dark side ... management), it’s fun to refresh and relearn all fun skills I used to know cold. One recommendation, the background music track is a bit distracting as well as the intro and trailing high volume soundtrack. Your content is very good; you don’t need the fluff.

belesiu
Автор

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

RIP my Ears!

skiddo
Автор

You're amazing and I'm so glad I found your channel. You taught me more about bit fields and extern than I've ever known - thank you sir!

tacolotto
Автор

Again, a really good episode!
I have an annotation related to the compiler's error behavior: For GCC you can control with the "-fno-common" option if more than one tentative definition for the same object are allowed. (Tentative definitions are non-static global definitions without "extern" and without initializer) . If you specify -fno-common, you need the "extern" keyword in all but one translation units. By this, you have only one definition and further declarations. I recommend to turn on this option if it is not already the default. The reason for this is, that it helps you to avoid errors if you unintentionally name unrelated variables with the same name. However, it is often better to have getter and setter funtions (an API) if you want to share data between different translation units!

michaelkotthaus
Автор

STM32CubeIDE recently upgraded its ARM GCC toolchain to a version whose linker now has its default behavior changed such that you must use the extern keyword for shared globals. So in a way, 'the old days' are back. (-fno-common is now default)

Imagine the confusion when my project suddenly wouldn't link anymore with the error that a few global variables had multiple definitions...

Hirnlappen
Автор

Generally when I make a declaration of a extern variable I do it in a header file. This gives the compiler a chance to throw an error if the declaration does not match the definition so long as the header file is included in the file where the variable is defined and also in any and all files where it is used. Compiler errors are generally more descriptive/helpful than linker errors.

peircedan
Автор

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

jamespong
Автор

What's the usage of *extern* keyword in function declarations (generally seen in header files)? How does *extern* make a difference in this case?

akshdeepsingh
Автор

Things I got from this: 1. I now know what a translation unit is 2. extern doesn't seem to do anything, you could just declare a global variable in a header, use the header file in your main program and just work with the variable.

WistrelChianti
Автор

Prof Sorber coming in with the best explanation.

tranqilo
Автор

If it's safest to use local variables, when is it recommended I actually use a global variable? What situation would this be better used than a local variable?

Drqonic
Автор

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?

kirbyquartz
Автор

This was the best extern video i could find, thanks that helped!

grownupgaming
Автор

So if I understand correctly, extern avoids the linker to throw an error because of a lack of definition of the variable in the current translation unit, but tells it to search for the identifier in the other units instead. Am I right?
Can it be applied to global variables (not to local ones) only?
Can it be applied to functions' prototypes?
Thanks

Neuromante
Автор

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

AV
Автор

At 1:08 you say you "declared a variable as extern and that you are going to use it, but it's actually "declared" somewhere else." Do you mean "defined" somewhere else or did you really mean "declared" as in two different places? Thank you.

carriersignal
Автор

extern can also be used for functions, not just variables

sidekickrida
Автор

Hello. I created a global variable of type struct. And both definition of structure and extern variable are in the same header file. When I try to share the same variable in different files, the extern global struct is not working as expected, it loads wrong value in registers. It's not copying the padding information. Removing padding fixed the issue, but is there any other way I can safely share extern structs. Embedded project. Arm compiler gcc 12 something compiler. Watch expression was showing correct value but register loading was wrong and sizeof gave different values in different c files, even if same header file was used. Also struct is a nested struct.

MsSuperasdfghjkl
welcome to shbcf.ru