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.
- Remove addNumber function in add.h.
- 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:
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!
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[…]
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[…]
GOLDEN RETRIEVER PUPPIES FOR SALE
[…]usually posts some really exciting stuff like this. If youre new to this site[…]
buy crabs online
[…]very handful of sites that transpire to be comprehensive below, from our point of view are undoubtedly effectively worth checking out[…]
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 […]
g spot vibrator review
[…]just beneath, are several completely not related internet sites to ours, having said that, they are surely worth going over[…]
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[…]
clit vibrator
[…]we came across a cool website that you may possibly take pleasure in. Take a appear in the event you want[…]
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[…]
prostate milking vibrators
[…]we came across a cool web page that you just may enjoy. Take a appear when you want[…]
best strap on for beginners
[…]the time to study or visit the content or web sites we’ve linked to below the[…]
Buy Weed Online
[…]the time to read or stop by the material or web-sites we’ve linked to beneath the[…]
Brazilian hair straightening
[…]here are some links to websites that we link to because we think they are worth visiting[…]
sex toys
[…]Here are some of the websites we recommend for our visitors[…]
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 […]
thrusting anal vibe
[…]Here are several of the internet sites we recommend for our visitors[…]
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[…]
Myths and truths about sex
[…]below youll locate the link to some web-sites that we believe you should visit[…]
electric motorcyles canada
[…]always a significant fan of linking to bloggers that I adore but dont get quite a bit of link adore from[…]
screen repair
[…]the time to read or take a look at the material or web pages we have linked to below the[…]