answersLogoWhite

0


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.

User Avatar

Wiki User

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

Wiki User

12y ago

A local variable is defined and declared within a block. It only has scope (visibility) within that block. Unless it is declared static, it only has lifetime within that block.

A global variable is defined and declared at file scope, i.e. outside of any block. It has scope to all blocks within that block. Unless it is declared static, it also has scope to all other modules that are linked to the file. Note that the meaning of static here is entirely different than the meaning of static within a block.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

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

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.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Their scope.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Difference between global and local variables?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Are C variables initialized to 0 by default?

Only global/static variables are, local variables aren't.


whose variables are also known as global variables?

The variables which are declared outside the main() function is known as global variables and they can be used anywhere in the program. And, the variables which used declare inside the main() function is known as local variables and they can be used inside the main() function only. Example: #include&lt;stdio.h&gt; #include&lt;conio.h&gt; int x,y; // global variables void main() { int a,b; // Local variables ------------ ---------------------- --------------------- getch(); }


What are local variables and global variables?

Local variables: These variables only exist inside the specific function that creates them. They are unknown to other functions and to the main program. As such, they are normally implemented using a stack. Local variables cease to exist once the function that created them is completed. They are recreated each time a function is executed or called. Global variables: These variables can be accessed (ie known) by any function comprising the program. They are implemented by associating memory locations with variable names. They do not get recreated if the function is recalled.


Is declaration of variables allocated memory?

Constants, static variables and global variables are allocated in the program's data segment at compile time. Local variables are allocated on the stack at runtime. Variables cannot be allocated on the heap, you must use a constant, static variable, global variable or local variable to store the start address of a dynamic memory allocation. The variable must be a raw pointer or a reference handle (a smart pointer).


How can you declare global an local variables in flowcharts?

The simple answer is you don't. The primary purpose of a flowchart is to show the flow of execution through an algorithm, with all primary functions and decisions described in broad, abstract terms. There is no need to distinguish between local and global variables because that is an implementation detail -- it's far too low-level a concept for algorithm design. All variables utilised by an algorithm should essentially be declared up front at the start of the flowchart and should therefore be treated as being local to that particular flowchart. It doesn't matter where those variables came from, only that they be initialised accordingly. The decision to make a variable global comes much later, once the interactions between different flowcharts have been established. Even so, a global variable should only ever be considered when the variable represents a truly global concept. In the vast majority of cases, passing arguments into functions is often the better option. Especially when separate algorithms are intended to run concurrently because making a global variable thread-safe can seriously impact performance. But this is not a concern in algorithm design, they are only of importance to the implementers; the class designers.

Related questions

What are the differences between global variables and local variables in C plus plus?

Global variables can be seen in all blocks of your program, when local variables are visible only within the block where it's declared.


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 difference between auto and local variables in c?

Nothing.


Difference between global optimization and partial optimization?

essential diffrence between global and local optimization


What is the difference between local and global revision?

that they are not the same thing


Are C variables initialized to 0 by default?

Only global/static variables are, local variables aren't.


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


How do you hide local variables in c and give precedence to global variables inside the function?

Hi, I would like to answr the question.So, if you want the to give more precedence to global variables with respect to a local one.Just add a pair of curly braces in the local variable and by doing so u can access global variable.


What is the difference between a local and a global revision?

A local revision focuses on specific words, phrases, and sentences; a global revision addresses larger aspects of the work.


What is the difference between local and global memory allocation?

global memory it is decide outside of your scope and we can access it in all of your scope


whose variables are also known as global variables?

The variables which are declared outside the main() function is known as global variables and they can be used anywhere in the program. And, the variables which used declare inside the main() function is known as local variables and they can be used inside the main() function only. Example: #include&lt;stdio.h&gt; #include&lt;conio.h&gt; int x,y; // global variables void main() { int a,b; // Local variables ------------ ---------------------- --------------------- getch(); }


If most variables are local in a program instead of global the result is a program with low or high coupling?

Coupling is the interdependency of a program. A program that uses local variables is more independent than one that uses global variables. Therefore, the program would be considered to have lower coupling.