Understanding the Extern Keyword in C

Posted By on May 5, 2019



the extra 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 dot o extension each of these object files or the C file that goes with it this 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 X turn 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 shared across your whole program so they assume X turn so if they assume extra and then Y use it well if I add an initializer for this variable I'm gonna get an error having to initializers for the same variable doesn't work and the error is a duplicate symbol error which sometimes confuses students if I add extra then you get two benefits I still get an error what the error is more helpful it says hey Jacob you just initialize the next turn bad for my friend and I think this error message is actually more readable which is really nice the second benefit is 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 extra 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

Posted by Lewis Heart

This article has 12 comments

  1. 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?

    Reply
  2. 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)?

    Reply
  3. 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.

    Reply
  4. 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!

    Reply

Leave a Reply

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