- NEW!
Help answer this question below.
A global variable in C/C++ is a variable which can be accessed from any module in your program. You can define a global variable with a statement like this:
int myGlobalVariable;
This allocates storage for the data, and tells the compiler that you want to access that storage with the name 'myGlobalVariable'.
But what do you do if you want to access that variable from another module in the program? You can't use the same statement given above, because then you'll have 2 variables named 'myGlobalVariable', and that's not allowed. So, the solution is to let your other modules DECLARE the variable without DEFINING it:
extern int myGlobalVariable;
This tells the compiler "there's a variable defined in another module called myGlobalVariable, of type integer. I want you to accept my attempts to access it, but don't allocate storage for it because another module has already done that".
when a variable is declared before main it is global,but cannot be used in any other file by including the program as a header file.where as an extern variable can be used in other file by including it as header file...
EXTREN VARIABLE
In the C and C++ programming languages, an external variable is a variable declared with the keyword extern. There are two specific meanings to extern, which are dependent on context.
When applied to the declaration of a variable, extern tells the compiler that said variable will be found in the symbol table of a different translation unit. This is used to expose variables in one implementation file to a different implementation file. The external declaration is not resolved by the compiler, but instead by the linker, meaning that extern is a legitimate mechanism to install modularity at the compiler level.
GLOBAL VARIABLE
Global variables are used extensively to pass information between sections of code that don't share a caller/callee relation like concurrent threads and signal handlers. Languages where each file defines an implicit namespace eliminate most of the problems seen with languages with a global namespace though some problems may persist without proper encapsulation. Without proper locking code using global variables will not be thread-safe except for read only values in protected memory.
An extern is not actually a variable. It's a declaration that tells the c pre processor and assembler that the linker will resolve the extern to a variable that's declared in another module. If the extern isn't a standard type, the type must be declared in an include file so the pre processor will accept and the assembler will know how to align the variable. You may want to understand a bit more about multi-pass compilers, which is what c and c++ are, to better understand this.
variables global and extern,are both initialized to zero??
variables global and extern,are both initialized to zero??
sorry
i have no idea!
Who invented computer programming?
by Answerbag Staff on February 1st, 2011
| 1 person likes this
How do i use visual basic 2008 express?
by Answerbag Staff on August 21st, 2010
| 1 person likes this
How do I create a table in Microsoft SQL?
by Answerbag Staff on November 8th, 2010
| 1 person likes this
Are computer programmers better than most people?
by Mister_Bromide on January 16th, 2012
| 5 people like this
You make the programs for money? Lots of money?
by Mister_Bromide on January 9th, 2012
| 1 person likes this
You're reading What is the difference between Extern variable and Global variable.
Comments
I would love to be able to understand what you just wrote! Impressive answer though...how do you know these things?
by Babycakes on November 28th, 2007
I'm a programmer by trade, dear. About 25 years now. Bagging is just a hobby! :)
by Stableboy on November 28th, 2007
Thanks Stableboy....you know your stuff!
by Babycakes on November 28th, 2007