You may encounter with “extern” keyword often. It becomes prefix of many variables / functions. Its hard to understand them, after a research I will share what I understand, correct me if I am wrong.

Declaration of variables or functions

Simply declaring a function does not allocate memory for them. After encountering with this lines compiler understands that definition of such variable/function resides somewhere in the memory and continues to execute next lines. Declaration shares idea of datatype of variable, function arguments order, and their datatypes to be passed, return type of function etc… to user.

Definition of variables or functions

Here comes allocation of memory for variables/functions. When we start defining(writing body of) a function corresponding memory for such is allocated inside systems memory, it is same for in case of variable too. This definition may be done at same file or some other file.

Analyze with example

->

int variable;

Here 4 bytes or 2 bytes of memory is allocated for variable.

int function(int a, int b)

{

return a+b;

}

extern keyword with functions

In case for functions,

int function1(void)

and

extern int function1(void);

both are same. Since extern keyword extends the visibility of function throughout the file and we can declare functions for N number of times in .c or .h files but definition will be done at only single file.

extern keyword with variables

For defining variable look above at ‘->’ line. But how will you declare a variable without defining it. Many of you may give wrong answer as

int variable;

but correct way is,

extern int variable;

Here I am declaring the variable as many times as I want.

Moral:

For functions extern is implicitly available at beginning, but this is not for the case of variables, we need to explicitly say it is an extern variable (declared but defined somewhere).

Confused…!

Look at this examples,

1)

#include <stdio.h>

int variable;

int main(void)

{

var = 10;

puts("Over....!");

}

Analysis: No problem here, you all know why it is…?!

2)

#include <stdio.h>

extern int var;

int main(void)

{

puts("Over...!");

}

Analysis: No problem here, since we declared var but not used it

3) 

# include <stdio.h>

extern int var;

int main(void)

{

var = 10;

puts("Problem...!");

}

Analysis: We declared “var” but not yet defined(any where) and trying to allocate some value for region that is not yet defined

4)

#include <stdio.h>

# include "audhil.h"

extern int var;

int main(void)

{

var = 22;

puts("No problem");

}

Analysis: I defined “var” inside my own header file audhil.h, hence declared and defined no problem.

Where as audhil.h holds

int var = 10;

5) 

# include <stdio.h>

extern int var = 99;

int main(void)

{

puts("No problem...!");

}

Analysis: No problem here, I normally declared and defined variable here…

Final thoughts

1. declaration of variable can be done many times but definition is only once allowed.

2. extern keyword extends visibility of variable/function.

3. Since functions are always visible inside file, hence extern keyword not required explicitly for function declarations.

4. extern keyword before variable says that, it is only declared but not yet defined.

5. as a special case, when an extern variable declared and initialized, it is known as definition of variable.

Enjoy…!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s