What is the difference between local variable and global variable?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the difference between local variable and global variable??

27th Apr 2017, 1:33 AM
Vishwas Setty N
3 Answers
+ 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; }
27th Apr 2017, 1:45 AM
jay
jay - avatar
+ 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.
27th Apr 2017, 1:48 AM
⏩▶Clau◀⏪
⏩▶Clau◀⏪ - avatar
+ 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,
1st May 2017, 8:34 PM
Manik Shekhar
Manik Shekhar - avatar