Tuesday, July 1, 2014

Understanding 'Extern' keyword

Let me begin my first post with a topic on 'Extern' keyword in C

'Extern' keyword in C is used in two contexts: variable and functions.

Before delving into the usage of extern let's touch upon 'function declaration/definition' in C. I'm sure almost (everyone) of you will be aware of declaration/definition of a variable/function but for the sake of completeness of this post, I would like to clarify them.

Declaration of a variable/function declares that a variable/function exists somewhere in the program but memory is not allocated. And it also gives details about type of a variable and about the arguments, their order and the return type in case of a function. This is about the declaration of a variable/function. Coming to the definition of a variable/function: it allocates the memory for that variable/function. Therefore, we can think of definition as a super set of declaration.

Coming back to the main definition of EXTERN:

Extern keyword associated with a variable/function extends the visibility of a variable/function throughout the program. By default all C function declarations and definition have "extern" keyword prepended with them. This means that even though we don't use "extern" with function declaration/definition it is present there.

For example, When we write int foo(int arg1, int arg2);
the compiler internally treats this as extern int foo(int arg1, int arg2);

Same is the case with the definition of a function in C. Memory will be allocated to the function only during the definition of the function and this indicates the a function can be declared any number of times in header or source files (.h/.c files) but it can be defined only once. As "extern" extends the visibility of the functions, they can be called anywhere form the program provided the declaration is known. So this is about the "extern" keyword associated with functions.


Now let's delve into the usage of "extern" keyword with the variables.

Any variable declared with "extern" keyword could be used anywhere else in the program. Unlike the previous case with functions variables are not declared/defined as "extern" by default. A variable in 'C' without the "extern" keyword will be declared and also memory will be allocated whereas the variable with "extern" declaration is just a declaration and no memory will be allocated and it can be used anywhere else in the program.

Let us consider the following examples:

example 1:

int var;
int main()
{
     var = 25;
     return 0;
}

the above snippet is perfectly fine as 'var' is declared and defined.


example 2:

extern int var;
int main()
{
    return 0;
}
The above is also fine as the extern variable var is never used (it is just declared)

example 3:

extern int var;

int main()
{
     var = 10;
     return 0;
}

Here, as the extern variable 'var' is not defined no memory is allocated. Compiler throws a undefined reference err in this case.

exmaple 4:

#include "SomeFile.h"

extern int var;
int main()
{
     var = 5;
     return 0;
}

This compiles successfully provided the var is defined in "SomeFile.h"

example 6:

extern int var = 50;

The above statement both declares and defines in a single statement and hence the future referral or updation to the 'var' will not throw any error.

So this is about some insight to "Extern keyword in C'
I hope it is helpful in some way and I have not disappointed :)

To summarize:


1. Declaration can be done any number of times but definition only once.
2. “extern” keyword is used to extend the visibility of variables/functions().
3. Since functions are visible through out the program by default. The use of extern is not needed in function declaration/definition. Its use is redundant.
4. When extern is used with a variable, it’s only declared not defined.
5. As an exception, when an extern variable is declared with initialization, it is taken as definition of the variable as well.


Citations:
http://www.geeksforgeeks.org/understanding-extern-keyword-in-c/
http://en.wikipedia.org/wiki/External_variable


No comments:

Post a Comment