-2

Possible Duplicate:
What are extern variables in C?

What is the difference between extern storage class and global variables in C programming language.? To me it seems like they both are same thing .Please clarify.

Community
  • 1
  • 1
user1543957
  • 1,758
  • 4
  • 20
  • 30

1 Answers1

1

an extern variable is a declaration of a variable which is defined in another unit.

You declare it in .h file:

extern int global_var;

and if you want to use it in .c file you define it in the global scope. And you should include the .h file in the source file that uses it.

Global variables are ariables which were declared outside of a block. They can be accessed everywhere in the program.

Please note that it is very important to know the difference between Declaring a variable and Defining it:

  • Declare a variable - There is something with this name, and it has this type. The compiler can use this variable without the need of all the definition of it.
  • Define a variable - Provide all of the information to create this variable.
Maroun
  • 94,125
  • 30
  • 188
  • 241