Storage classes in C - extern

Storage classes in C tell the compiler how to store the variables. When declaring variables with storage classes you use the following syntax

storageType  dataType  variableName;

Difference between variable declaration and definition

Let me tell you a story

You are supposed to buy a laptop and you need Rs 40,000 for that. You have Rs 30,000 with you and you need Rs 10,000 more. Your friend assures you that he will meet you at the laptop shop tomorrow and will give you Rs 10,000 there and so you should call the laptop shop and book your order. But remember it's just an assurance. There can be a chance that he might not be able to come there and therefore you will not be able to buy the laptop.

In the above story you have Rs 30,000 with you and for Rs 10,000 you depend on some external source which will meet you later on.
Declaration is a assurance we make to the compiler. It's like saying that we do not have the variable in memory right now but you will get it later on at the time of execution and so you should continue working.

Definition actually creats the variable.

int  i ;  // Declaration and definition    char c ; // Declaration and definition    int Sum ( int a , int b );  // Just declaration. There is no                                        // body created for the function.

The concept of linking

There are two steps to running a C program - Compiling and linking. The first step is compiling where the compiler will check the program and makes sure that it follows all the rules of the language. Linking is the second step which happens when you run the program. At this time the linker will make sure that you have everything you need in order to run the program. If something you need is not there then you have a linker error and the program will not run.

In the story above you order the laptop because your friend has assured to meet and tomorrow and give you Rs 10,000. Therefore you feel that everything is OK and you place the order. So technically speaking the program compiles fine.

But tomorrow when you reach the shop and you find that your friend cannot come to give you the money you have a linker problem. This is because the external source on which you depended earlier could not be found at the time of execution.

But if your friend shows up at the laptop shop and gives you Rs 10,000 you have a successful linking and you are able to buy the laptop.

Linking is always done when you are about to run the program.

The extern storage class

Always remember

  1. extern is an assurance. It can be broken at the time of linking !
  2. extern is a declaration. The definition is somewhere else in the program.

When you mark something as extern you are assuring the compiler that the actual definition ( memory allocation ) of this variable is done somewhere else in the program and at runtime the linking will be done with its definition and therefore the compiler should compile the program for now.

For example

void Fun ()  {      extern int i; // This is just declaration - assurance      printf("%d", i);  }

When you compile the above program it will give you no error.

But when you run it, you will get linker error because you have only declared the variable i and it has not been defined ( allocated memory ) anywhere in the program. Neither is it inside the function body and nor is it outside the function body.

Lets write the above program again

void Fun()  {      extern int i;  // This is just declaration - assurance      printf("%d", i);  }  int i = 10;  // This is definition. We are creating the                    // variable i and allocating it memory here.

The program above will compile and run properly. Because at the time of execution it will be able to find the variable i in memory as it has been defined outside the body of the function.

extern is used to tell the code that the variable has been defined ( allocated memory ) somewhere else in the program and it will be found at the time of linking.

 

Related Questions

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp