Home > DeveloperSection > Articles > Objective C - Understanding “extern” keyword

Objective C - Understanding “extern” keyword


iPhone iPhone  Ios  Objective C 
Ratings:
0 Comment(s)
 945  View(s)
Rate this:

Objective C - Understanding “extern” Keyword


Keyword extern is used for declaring extern variables in Objective-C. This modifier is used with all data types like int, float, double, array, pointer, function etc.


 Basically extern keyword extends the visibility of the C variables and C functions. Probably that’s is the reason why it was named as extern.


Declaration of a variable/function simply declares that the variable/function exists somewhere in the program but the memory is not allocated for them. But the declaration of a variable/function serves an important role. And that is the type of the variable/function. Therefore, when a variable is declared, the program knows the data type of that variable.


 In case of function declaration, the program knows what are the arguments to that functions, their data types, the order of arguments and the return type of the function. So that’s all about declaration.

 

Coming to the definition, when we define a variable/function, apart from the role of declaration, it also allocates memory for that variable/function.

 

Let us first take the easy case. Use of extern with Objective C functions:

By default, the declaration and definition of a Objective-C function have “extern” prepended with them. It means even though we don’t use extern with the declaration/definition of Objective-C functions, it is present there. For example, when we write:

int foo(int arg1, char arg2);

 There’s an extern present in the beginning which is hidden and the compiler treats it as below.

extern int foo(int arg1, char arg2);

 

Same is the case with the definition of a Objective-C function. Therefore whenever we define a Objective-C function, an extern is present there in the beginning of the function definition. Since the declaration can be done any number of times and definition can be done only once.

Now let us the take the second and final case i.e. use of extern with C variables:

I feel that it more interesting and information than the previous case where extern is present by default with Objective-C functions. So let me ask the question, how would you declare a Objective C variable without defining it? Many of you would see it trivial but it’s important question to understand extern with Objective-C variables. The answer goes as follows:

extern int var;

 

Here, an integer type variable called var has been declared (remember no definition i.e. no memory allocation for var so far). And we can do this declaration as many times as needed. (remember that declaration can be done any number of times) So far so good. :)

 

Now how would you define a variable. Now I agree that it is the most trivial question in programming and the answer is as follows.

int var;

 

Here, an integer type variable called var has been declared as well as defined. (remember that definition is the super set of declaration). Here the memory for var is also allocated. Now here comes the surprise, when we declared/defined a Objective C function, we saw that an extern was present by default. While defining a function, we can prepend it with extern without any issues. But it is not the case with variables. If we put the presence of extern in variable as default then the memory for them will not be allocated ever, they will be declared only. Therefore, we put extern explicitly for variables when we want to declare them without defining them. Also, as the extern extends the visibility to the whole program, by externing a variable we can use the variables anywhere in the program provided we know the declaration of them and the variable is defined somewhere.

Now let us try to understand extern with Objective-C examples.

Example 1:

#import <Foundation/Foundation.h>
int var;
int main (void)
{
   var = 10;
   return 0;
}

 

Analysis: This program is compiled successfully. Here var is defined (and declared implicitly) globally.

Example 2:

#import <Foundation/Foundation.h>

extern int var;

int main (void)

{

   return 0;

}

Analysis: This program is compiled successfully. Here var is declared only. Notice var is never used so no problems.

Example 3:

#import <Foundation/Foundation.h>

extern int var;

int main (void)

{

   var = 10;

   return 0;

}

Analysis: This program throws error in compilation. Because var is declared but not defined anywhere. Essentially, the var isn’t allocated any memory. And the program is trying to change the value to 10 of a variable that doesn’t exist at all. 

Example 4:

#import <Foundation/Foundation.h>

#include "somefile.h"

extern int var;

int main (void)

{

   var = 10;

   return 0;

}

Analysis: Supposing that somefile.h has the definition of var. This program will be compiled successfully.

Example 5:

#import <Foundation/Foundation.h>

extern int var = 0;

int main (void)

{

   var = 10;

   return 0;

}

Analysis: Guess this program will work? Well, here comes another surprise from Objective-C standards. They say that..if a variable is only declared and an initializer is also provided with that declaration, then the memory for that variable will be allocated i.e. that variable will be considered as defined. Therefore, as per the Objective-C standard, this program will compile successfully and work.

 

In short, we can say


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.

 


Don't want to miss updates? Please click the below button!

Follow MindStick