answersLogoWhite

0

What does it mean for a variable to be global?

Updated: 8/17/2019
User Avatar

Wiki User

15y ago

Best Answer

A global variable can be accessed from any area of your program. This differs from a local variable since local variables can only be accessed (and only exist) inside the function that declared them. For example:

int global_var = 0; // Declared globally

void function_x()

{

int local_var = 0; // Declared locally (inside function_x)

global_var = 1;

}

int main()

{

function_x();

// This will print "global_var = 1"

std::cout << "global_var = " << global_var << endl;

// This will cause a compiler error "local_var undeclared" since local_var only exists inside function_x

std::cout << "local_var = " local_var << endl;

return 0;

}

The reasons for doing this is so that you don't have functions modifying variables that they should not have access to.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What does it mean for a variable to be global?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

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


How can you declare global and local variable 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.


Why static variables in a function have global extent?

A variable declared static outside of a function has a scope the of the source file only. It is not a global variable. A variable declared outside of a function and without the static qualifier would be a global variable.


How do you access global variable from within the main function when the local variable is of the same name?

When you acess a global variable inside main function you must use the same name, because the variable declared as global can be accessed by any function/procedure on the class where it was defined.


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.


Where global variable get register?

const


What is the syntax of global variable in java?

There's no global variables in Java.


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.


What is Global variables in vb6?

Global variables are accessible through your project. Different classes, modules, and interfaces can access a variable if it is global. A Global Variable can simply be declared by putting the word Public in front of the variable. Here is an example. Public V As String Now anything in your whole entire project can access the variable V.


If the variable flag can only be declared once in the program it was declared as what type of variable?

It's a global variable.


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.