How to correctly use the extern keyword in C

Cover Image for How to correctly use the extern keyword in C
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

How to correctly use the extern keyword in C 🌐

Are you confused about the extern keyword in C? 😕 You're not alone! Many developers find it tricky to understand when and how to use it. But fear not! In this blog post, we'll demystify the extern keyword and give you practical examples to help you understand its proper usage. Let's dive in! 💪

Understanding the purpose of extern 🔍

The extern keyword is used to declare a variable or function that is defined in another source file or library. It is a way to tell the compiler that the declaration being made is not local but is located elsewhere. This is particularly useful when you want to access variables or functions that have global scope but are defined in a different file.

Common issues and their solutions 🛠️

Issue #1: Difficulty accessing global variables

Let's say you have a global variable defined in one source file but need to access it in another. Without the extern keyword, the compiler will throw an "undefined reference" error. To fix this, you can use the extern keyword in the source file where you want to access the variable:

// File1.c
int globalVariable = 10;

// File2.c
extern int globalVariable;

Now, you can use globalVariable in File2.c without any issues.

Issue #2: Using functions across multiple source files

If you have a function defined in a different source file and want to use it in your current file, you'll face a similar issue. The extern keyword comes to the rescue again! Use it to declare the function in your current file before using it:

// File1.c
void externalFunction(); // Function definition

// File2.c
extern void externalFunction(); // Function declaration

int main() {
  externalFunction(); // Function call
}

By declaring the function as extern, you can use it without any problems in the current file.

The extern keyword in header files 📝

When it comes to header files, things get a bit different. In most cases, header files already provide the necessary declarations for functions and variables. Therefore, using extern in header files is generally unnecessary.

However, if you have a global variable declared in a header file, and want to access it in multiple source files, you can declare it as extern in the header file and define it in one of the source files:

// Header.h
extern int sharedVariable;

// File1.c
#include "Header.h"
int sharedVariable = 42;

// File2.c
#include "Header.h"

void someFunction() {
  printf("Value of sharedVariable: %d", sharedVariable);
}

In this case, the extern keyword in the header file signals that the definition of sharedVariable is located elsewhere.

Time to master the extern keyword! 💡

Congratulations! You now have a solid understanding of how and when to use the extern keyword in C. Keep in mind the following key points:

  • Use extern when accessing global variables or functions defined in other source files.

  • Avoid using extern in header files unless you specifically need to declare a global variable that will be defined in a separate source file.

If you still have any doubts or questions, feel free to leave a comment below. Happy coding! 💻✨


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello