+ 1
What is the difference between local variable and global variable??
3 Respostas
+ 5
the local can only be accessed locally. the global can be accessed anywhere thus is global
int global;
void function();
int main() {
int local;
local = 5;
global = 10;
functon();
cout << local; // 5
cout << global; // 11
return 0; }
void function() {
local = 6; // error. not acceseable
global = 11;
}
+ 5
Local variables have scope in the block of code where are defined. Global variables have scope beyong the block of code where are defined. Globals are advised against been used, because produce code difficult to maintenance.
+ 1
1.
GV: Global variable declared inside the class body.
LV: Local variable declared inside the constructor body or method body.
2.
GV: They get memory inside the object.
LV: Get memory at the time of method or constructor call.
3.
GV: If GV is not initialize we'll get default value from the compiler.
LV: Before access LV, it must have to initialize,