• Computer Programming
  • Meteorology and Weather
  • C Programming

Difference between Global and Local variable?

User Avatar

Wiki User

βˆ™ 2011-05-05 11:08:46

Best Answer

The scope or the life span. The life span is from the birth (allocation) to death (deallocation).

The life span of a global variable starts when the application is invoked. It dies when the application terminated. During the execution of the application , this global variable is available to any program unit within that application.

The life span of a local variable, well, only locally. "Local" means a component, a method (subroutine), or even within a bracket ({} or BEGIN-END) of a statement.

Outside of that block of codes, that local variable does not exist, hence cannot be accessed or referenced. Also, the same name of that local variable may be declared again in another local area without conflict, nor memory of the previous one.

For languages similar to C:

for (int i = 0; i < 100; i++)


// i can be referenced within { }, and within the for() control itself


for (int i = -1; i > -200; i--)


// this i is different from the i in the previous for-loop


Some computer language would allow you to declare a local variable with the same name as the global one (for example, there are global::X and local::X, but the code only refer as X). Most of the computer language would replace X with local::X at compile time, some would replace it with global::X) Some languages may not have the notion of local variables, everything is global, and some would not have the notion of global variable, but would provide a mean to act like one.

2011-05-05 11:08:46
This answer is:
User Avatar

Add your answer:

Earn +5 pts
Q: Difference between Global and Local variable?
Write your answer...

Related Questions

What is the difference between global and local variable?

Their scope.

What is the difference between static variable and global variable declaration?

When you say static variable, do you mean local static variable or global static variable? In C, the difference between global static variables and global variables is that static in this case means that the variable can be used only in the module (.c file) that it is declared. The difference between a local static variable and a global variable is the scope: the local variable can be used only inside the function that declares it. So you can have 2 local static variables in the same file with the same name but in different functions and they will be 2 different variables.

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.

Is a global variable a non-local variable?

True, a variable cannot be both global and local. But if a global and a local variable share the same name, the local one will hide the global.

What is the difference between auto variable and local variable?

They are the same.

What is the difference between local and global variable of C language?

4m Sai. Gloabal Variable is variable which is declared before main (). int a=10; main() { } here a is a global variable. where ever u r using this variable ... we'll get the value as 10. Local Variable is a variable which is declared inside the main(). int a=10; main() { int a=5; ........ ....... } here a=5 is a local variable and a=10 is called as global variable. then if u want to get a value... printf("%d",a); then result 'll be a=5; B'cos compiler gives main preference to local variables.. if any local declareation is not there then it 'll prefer global variable.

What is difference between global and local memory?

If you have global memory it is defined outside of your scope and you can access it in all of your scopes. If you have local memory it is defined within a scope and can ONLY be accessed in that particular scope. If you have a global and a local variable with the same name the local will be selected.

Difference between static variable and automatic variable?

In C programming, a static variable declared outside of any function is global, but local to the current module. A static variable declared within a function retains its value between function calls.

What is the difference between local variable and global variable in Embedded C?

It's simple. A global variable has a scope through out out the C program. It can be accessed anywhere from any function or etc. A local variable on the other hand, is local to it's container only and can not be accessed outside of it's container. For example a function has variable sum then sum is only accessible within the function and not anywhere else.

Difference between global and local variables?

A global variable remains in scope throughout the life-cycle of the program. This exposes the variable to uncontrolled changes from any code that has access to it. A local variable is scoped within a block of code (a function or statement block) and will fall from scope when the code block falls from scope. Thus only the code block can alter the variable. A static local variable is a bit like a global in that it exists throughout the program's life-cycle, however it is still a local variable and therefore has limited scope. The only real difference is that the variable retains its value between calls to the code block, whereas a non-static local variable is destroyed when the code block falls from scope.

What is a local variable?

Local variable is a variable having local scope. Local variable has higher priority than global priority.

Difference between global optimization and partial optimization?

essential diffrence between global and local optimization

How can you declare global and local variables in pseudocode?

A local variable is a variable declared inside a construct, such as a class or function, while a global variable is a variable declared outside of any construct.

Difference between global variables and local variables?

Their scope.

Difference between localand global variable in c plus plus?

#include func_1(...); var2 = 0;//global variable which is visible for func_1 as well as main(){...double var = 0.0;//local variable which is visible only inside of main...}int func_1(...){ var1 = 0;//local variable which is visible only inside of func_1}

Difference between local and global variable?

Local variables are allocated on the stack while global variables are allocated in the program's data segment. A local variable is scoped to the code block in which it is declared. Local variables are created when the scope becomes active and destroyed when the scope ends. Local variables can be scoped to an entire function, a code block within a function or to a class (nonstatic member variable). A global variable exists for the entire duration the program is running and is accessible from any code. The scope of a global is the translation unit in which it is declared, however any other translation unit can grant itself access to a global variable by declaring that variable as an external variable. Global variables are generally frowned upon since the author of the translation unit has no control over which translation units may grant themselves access, making it difficult to determine when, where and why a global variable is changing value. Global constants are less of a problem since the value cannot change.

What the difference between global winds and local winds?

my nuts you fagots

What is the difference between local revision and global revision?

that they are not the same thing

How are global variable different from local variable in Visual BASIC?

A global variable is available for use throughout the program. It is normally declared outside any procedure. A local variable is only available for use in the procedure it was declared in. Most variables are local, but there will be occasions when you want different procedures to be able to use the same variable. This is when you use a global variable.

How can you declare global and local variable in pseudocode?

ad1: 'global variable n' ad2: 'local variable m' Note: don't forget pseudocode means human readable text

What is the difference between domestic politics and global politics?

Yes. Domestic is local or national and global is the world. That's why it is called "global."

What is a local variable and What statements are able to access a local variable?

A local variable is a variable that can only be called on by the module. Where as a global variable can be called upon by any module. Only statements made inside the same module can call on a local variable.

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.

Difference between local variables and global variables?

The scope (life span) and the accessibility. Global variables has the longest life span of any variables in other scope (local, temp.). Conceptually it lasts the duration of the application execution. A global variable should be "globally" accessible. That is, from anywhere in the application, any unit, and any module. A local variable lives within the scope - locally. The scope starts with the method (procedure, subroutine, function) being invoked and ends with the end of the subroutine, and hence conceptually and normally a local variable should not be globally accessible.

Difference between global variable and local variable?

I'm assuming you're referring to computer programming. A global variable can be accessed by all functions. It is initialized at the beginning of the program and is deleted when the program shuts down. A local variable is isolated in its function. Here's an example in C: #include &lt;stdio.h&gt; int other_function(); int global_int = 2; /*This is a global variable that can be accessed by any function in the program.*/ int main( int numArgs, char *argList[] ) { int local_int = 1; /*This is a local variable. It can only be accessed inside of main(). Once main() ends, the variable is deleted and the data is erased.*/ printf( "Global variable is %i. Local variable is %i.\n", global_int, local_int ); other_function(); return 0; } int other_function() { printf( "Global variable is %i. The variable "local_int" located in main() can not be accessed. This line of code will return an error %i", global_int, local_int ); /*This line attempts to access local_int, which does not exist because it is available only to the function main()*/ return 0; } I hope tat answered your question.