How to Understand “extern” Keyword in C (example)

If you know the “extern” keyword, but you are not clear about where and why do we need it. You are in right place.

“extern” can be used to declare variables and functions which are defined in other files. so we can use these variables and functions in this file.

The following case can let you understand why we need “extern” keyword. I used “extern” function as example, the same with variables.

First, In case 1, please let me show you how to invoke a function in other files.

Note: Run the program in Ubuntu 16.04 LTS.

Case 1:

There are four files in case 1: add.h add.c multi.h mult.c main.c.

In add.c

#include <stdio.h>
#include "add.h"
int addNumber(int a, int b) 
{
	return a + b;
}

In add.h

#ifndef __ADD_H__
#define __ADD_H__

int addNumber(int a, int b);

#endif

In multi.c

#include <stdio.h>
#include "multi.h"
#include "add.h"
int multiNumber(int a, int b)
{
	int i = 0, res = 0;
	for (i = 0; i < a; i++) {
		res = addNumber(res, b);
	}
	return res;
}

In multi.h

#ifndef __MULTI_H__
#define __MULTI_H__

int multiNumber(int a, int b);

#endif

In main.c

#include <stdio.h>
#include "multi.h"
#include "add.h"
int main()
{
	int a = 4, b = 5;
	int res = 0;
	res = addNumber(a, b);
	printf("addNumber res = %d\n", res);
	res = multiNumber(a, b);
	printf("multiNumber res = %d\n", res);
	return 0;
}

According to the above code, we know that we can invoke addNumber function only if we add add.h file. because addNumber is exposed in add.h file, right?

NOW!

There is a question: How can we invoke addNumber only in multi.c? How?

“extern” keyword is used now.

Case 2:

Base on Case 1, we only modify the following two places.

  1. Remove addNumber function in add.h.
  2. Add extern keyword to declare addNumber in multi.c.

In add.h

#ifndef __ADD_H__
#define __ADD_H__

//int addNumber(int a, int b);

#endif

In multi.c

#include <stdio.h>
#include "multi.h"
#include "add.h"

extern int addNumber(int a, int b);

int multiNumber(int a, int b)
{
	int i = 0, res = 0;
	for (i = 0; i < a; i++) {
		res = addNumber(res, b);
	}
	return res;
}

After you do that, you will get the following error info:

error info

Because we didn’t expose addNumber function in add.h, so complier can’t find this function in main.c. But we use extern keyword to declare addNumber in multi.c, so multiNumber function can call addNumber function in multi.c.

Now, We remove res = addNumber(a, b); in main.c, then make and run main, we will get the following result.

multiNumber res = 20

What I learned from these cases:

Using “extern” keyword, we don’t need expose this function to all other files. Only when we want to use it, we use extern keyword to declare it. It is safty.

Hope this post is useful for you to understand “extern” keyword in C.

Have a good day!

If you have any question, please feel free to let me know. I will appreciate it if you give me some feedback!

Share this article to your social media
Subscribe
Notify of
guest
373 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
trackback
Buy Weed Online
2 years ago

Buy Weed Online

[…]although internet websites we backlink to beneath are considerably not related to ours, we feel they are basically really worth a go as a result of, so possess a look[…]

trackback
PARROTS FOR SALE
2 years ago

PARROTS FOR SALE

[…]very couple of sites that come about to be comprehensive below, from our point of view are undoubtedly effectively worth checking out[…]

trackback
GOLDEN RETRIEVER PUPPIES FOR SALE
2 years ago

GOLDEN RETRIEVER PUPPIES FOR SALE

[…]usually posts some really exciting stuff like this. If you’re new to this site[…]

trackback
buy crabs online
2 years ago

buy crabs online

[…]very handful of sites that transpire to be comprehensive below, from our point of view are undoubtedly effectively worth checking out[…]

trackback
THC Cartridge
2 years ago

THC Cartridge

[…]Wonderful story, reckoned we could combine several unrelated information, nevertheless seriously really worth taking a look, whoa did one learn about Mid East has got more problerms at the same time […]

trackback
g spot vibrator review
2 years ago

g spot vibrator review

[…]just beneath, are several completely not related internet sites to ours, having said that, they are surely worth going over[…]

trackback
nba hoop troop arcade game
2 years ago

nba hoop troop arcade game

[…]we came across a cool internet site that you just could possibly delight in. Take a look in the event you want[…]

trackback
clit vibrator
2 years ago

clit vibrator

[…]we came across a cool website that you may possibly take pleasure in. Take a appear in the event you want[…]

trackback
smash or pass sex toy edition
2 years ago

smash or pass sex toy edition

[…]always a huge fan of linking to bloggers that I like but do not get lots of link appreciate from[…]

trackback
prostate milking vibrators
2 years ago

prostate milking vibrators

[…]we came across a cool web page that you just may enjoy. Take a appear when you want[…]

trackback
best strap on for beginners
2 years ago

best strap on for beginners

[…]the time to study or visit the content or web sites we’ve linked to below the[…]

trackback
Buy Weed Online
2 years ago

Buy Weed Online

[…]the time to read or stop by the material or web-sites we’ve linked to beneath the[…]

trackback
Brazilian hair straightening
2 years ago

Brazilian hair straightening

[…]here are some links to websites that we link to because we think they are worth visiting[…]

trackback
sex toys
2 years ago

sex toys

[…]Here are some of the websites we recommend for our visitors[…]

trackback
buy kratom
2 years ago

buy kratom

[…]Every as soon as in a while we pick blogs that we read. Listed below are the most recent sites that we choose […]

trackback
thrusting anal vibe
2 years ago

thrusting anal vibe

[…]Here are several of the internet sites we recommend for our visitors[…]

trackback
lifelike dildo
2 years ago

lifelike dildo

[…]although web-sites we backlink to below are considerably not connected to ours, we really feel they’re essentially really worth a go through, so possess a look[…]

trackback
Myths and truths about sex
2 years ago

Myths and truths about sex

[…]below you’ll locate the link to some web-sites that we believe you should visit[…]

trackback
electric motorcyles canada
2 years ago

electric motorcyles canada

[…]always a significant fan of linking to bloggers that I adore but don’t get quite a bit of link adore from[…]

trackback
screen repair
2 years ago

screen repair

[…]the time to read or take a look at the material or web pages we have linked to below the[…]