answersLogoWhite
Computer Programming
Meteorology and Weather
C Programming

Difference between Global and Local variable?


Top Answer
User Avatar
Wiki User
2011-05-05 11:08:46
2011-05-05 11:08:46

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.

Related Questions

User Avatar

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.

User Avatar

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.

User Avatar

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.

User Avatar

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.

User Avatar

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.


Copyright © 2020 Multiply Media, LLC. All Rights Reserved. The material on this site can not be reproduced, distributed, transmitted, cached or otherwise used, except with prior written permission of Multiply.