answersLogoWhite

0


Best Answer

Global variables are non-local variables. That is, variables that are not defined in a function or class. They are globally accessible to all code in the same translation unit. External variables are global variables that have external linkage; they are accessible across translation units.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

A global variable is a variable that is local to a file. An extern variable is a global variable that is defined in a file other than the one in which it is declared. Since programmers have no overall control over which files may declare a global as being an external global, globals are frowned upon. If a variable needs to be accessible to code outside of the file in which it is declared, it should be declared within an enclosing scope (such as the program's main function) and passed directly to the code that requires it. This ensures that only code that requires access to the variable can gain access to the variable, making it easier to determine where and how a variable is being changed and why it is being changed.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between global variable and extern variable?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the difference between global and extern?

AnswerGlobal variables and extern variables are very similar, but with a major difference. Let's say you have a global variable in your header file, like this:( Refer how u can add the variable declaration in the source code as extern, i think we cant add into header files as it said)int x = 0;Now, in each file that includes that header file, there will be a new variable called x. Modifying x in file1.cpp won't modify x in file2.cpp.Now, for externs... Say you have something like this in your header:extern int x;Then, in file1.cpp (which includes your header):int x = 0;Then, in file2.cpp (also includes the header):x = 46;x in file1.cpp is also now equal to 46. You see, when you use an extern variable, you're telling the compiler that you want to use that variable across all of your source files. If you use a regular global, however, you create a new variable in each of your source files.Anyway, that's just for variables, which I assume you're talking about - global and extern functions are a bit different, and I'm not entirely clear on them.


What is difference between Static and Global variable?

Static may be local of global -local static variable is limited to the function scope. and retain it's value when function is been called . compiler differentiate static variables with a prefix function name while dealing with same name static variable in different functions. - Global static variable is visible to all the function defined in the file and retain it value but it cannot be used outside this file. now Global Variable --- this variable is also visible to all of the functions inside the file but also can be used outside the file via extern keyword.


What is external variable in c language?

The "extern" declaration in C is to indicate the existence of, and the type of, a global variable or function. A global variable, or a global function, is one that is available to all C modules (a single C module is typically a single .c file). An extern is something that is defined externally to the current module. In many cases, you can leave off the extern qualifier and not notice any difference because the linker can collapse multiple definitions to one. But the intent is then unclear in the code, and the code is error prone in case of typos. It is much clearer to define the global in one place, and then declare extern references to it in all the other places. When refering to globals provided by a library, especially a shared library, this is even more important in order to ensure you are talking about the correct, common instance of the variable. Declaring a variable as extern will result in your program not reserving any memory for the variable in the scope that it was declared. For instance (as example) if a program's source code declared the variable var as a global volatile int in foo.c, to properly use it in bar.c you would declare it as extern volatile int var. It is also not uncommon to find function prototypes declared as extern. A good C manual will certainly answer this more completely.


What is the difference between a static variable a global variable and a local variable?

A static variable is a variable allocated in static storage. A local variable is a variable declared inside a function. A global variable is a variable declared outside of any class or function. Note that local variables and global variables can both be allocated in static storage.


What is the difference between a global variable and a private variable?

The accessibility. The global one: almost everywhere in the code may reference to the global variable directly. The private variable, is private to the declaring module (class, method, assembly) only. Outside of that module has no access to it directly.


What is the difference between extern and global?

Extern and Global are the storage space in C program. Global provides us to access the variables from anywhere inside the program only whereas Extern provides us to access the variables from outside that program, i,e., some other program.


Can you use extern and static together?

Storage classes are used to indicate duration and scope of a variable or identifier. Duration indicates the life span of a variable. Scope indicates the visibility of the variable. The static storage class is used to declare an identifier that is a local variable either to a function or a file and that exists and retains its value after control passes from where it was declared. This storage class has a duration that is permanent. A variable declared of this class retains its value from one call of the function to the next. The scope is local. A variable is known only by the function it is declared within or if declared globally in a file, it is known or seen only by the functions within that file. This storage class guarantees that declaration of the variable also initializes the variable to zero or all bits off. The extern storage class is used to declare a global variable that will be known to the functions in a file and capable of being known to all functions in a program. This storage class has a duration that is permanent. Any variable of this class retains its value until changed by another assignment. The scope is global. A variable can be known or seen by all functions within a program.


Describe about storage allocation and scope of global extern static local and register variables?

Global Varible: The variable which is declared as "Global" one : having the preveleges to use and access that variable in any class and object( means any where in the program) just like PUBLIC keyword in OOPS concepts. Static Variable : If we declare a variable as Static , then it wont have the permission to access that variable through out the program and u have to use it inside the class or object which u declared itself. All the Best Annapurna


Difference between global and transnational strategy?

the difference between global and international strategy


How do you make a variables from outside a function work in that function?

declaring a variable before main() will be accessed in main as well as in function which is called a "global variable" or "external variable". *edit* a very good programming practice, which is also necessary with some compilers, is to declare the variable inside the function as well with the extern declaration before the type declaration. for example if you had this inside a function: int func(){ int x = 0; x++; return x; } that would be fine, but to declare it externally, you should include extern inside the function int x = 0; int func(){ extern x; x++; return x; } assuming that we declared the variable globally, which we did.


Is global variables are declared within the main function?

Might be, but don't forget the keyword 'extern':int main (void){extern int errno;...}


Difference between international and global communication?

what is the difference between international communication and global communication