by Parthee on November 27th, 2007

Parthee

Question

Help answer this question below.

What is the difference between Extern variable and Global variable.

  • Like
  • Report

Answers. 7 helpful answers below.

  • by Stableboy on November 28th, 2007

    Stableboy

    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".

    • Like
    • Report

    3 comments | Post one | Permalink

  • by manju on August 26th, 2009

    manju

    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...

    No comments. Post one | Permalink

  • by ankita on March 18th, 2009

    ankita

    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.

    No comments. Post one | Permalink

  • by dichosa on March 2nd, 2009

    dichosa

    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.

    No comments. Post one | Permalink

  • by Anonymous on March 1st, 2009

    Anonymous

    variables global and extern,are both initialized to zero??

    No comments. Post one | Permalink

  • by Anonymous on March 1st, 2009

    Anonymous

    variables global and extern,are both initialized to zero??

    No comments. Post one | Permalink

  • by duMpyz on November 27th, 2007

    duMpyz

    sorry
    i have no idea!

Want to attach an image to your answer? Click here.

Did this answer your question? If not, then ask a new question or create a poll.

More Questions. Additional questions in this category.

You're reading What is the difference between Extern variable and Global variable.

Follow us on Facebook!

Related Ads

ANSWERBAG BUZZ

Extern variable in c
Extern variable
C extern global variable
Extern variable c
External variables in c