Yes, local and global variables can have the same name in C++, but the local variable will take precedence within its scope, shadowing the global variable.
Key takeaways:
Local variables are declared inside a function or block, accessible only within that scope.
Global variables are declared outside functions and are accessible throughout the program.
Local variables are stored on the stack, created at function call, and destroyed when the function ends.
Global variables are stored in the data segment for the entire program’s duration.
Local variables act as local memory for functions; global variables serve as shared memory.
Use local variables to reduce memory overhead and prevent name conflicts.
Minimize global variable usage to avoid unintended side effects across functions.
In C++, variables are essential for storing and manipulating data, and understanding how they behave in different scopes is key for writing effective programs. Variables can be classified as local or global based on where they are declared and used within the code. In this Answer, we’ll understand the differences between local and global variables in C++, their scope, lifetimes, and best practices for using them.
#include <iostream>void func(){int localV = 10;std::cout<<localV;}
Conversely, the variables defined outside of any function or block of code are called global variables in C++. The scope of these variables is the complete C++ program written after the variable’s creation.
#include <iostream>int globarV;void setValue(int v){globarV = v;}void printValue(){std::cout<<"Value: " <<globarV;}
Local Variables | Global Variables |
Accessible in one function or block only. | Accessible in a complete program written after the variable’s creation. |
Stored on the Stack with the function calls during a program’s execution. | Stored in the data segment, neither on the Stack nor the heap, during a program’s execution. |
Used like local memory for a function/block. | Used as shared memory for all the functions and blocks. |
Here’s the coding example for local variables that will generate the compile-time error:
#include <iostream>using namespace std;void func1(){int localV = 10;// Legal to accesscout<<localV <<endl;}int main(){func1();// Illegal to access (Compile time error)cout<<localV <<endl;return 0;}
Lines 5 and 7: A local variable localV
is defined inside func1()
. It can be accessed inside this function, but not outside.
Line 13: If we access localV
inside main()
function, it will cause a compile-time error.
Note: There is a compile-time error in the above-given code snippet. To resolve that, please comment out the following line of code, cout<<localV <<endl;
, inside the main()
function.
Here’s the coding example for global variables:
#include <iostream>using namespace std;int globarV = 100;void func1(int v){// Legal to accessglobarV = v;cout<<"func1: " <<globarV <<endl;}int main(){// Legal to accesscout<<"main: " <<globarV <<endl;func1(-1);return 0;}
Line 4: A global variable globalV
is defined here. It can be used anywhere throughout this program, inside the code written afterward.
Lines 8–9 and 14: The globalV
is accessed inside func1()
and main()
functions. There is no compile-time error for that.
The local variables are short-term variables of the program. They are created on the Stack when a function’s execution starts. They are deleted by the end of that function’s execution. They behave like a local memory of a function.
The global variables are like long-term variables of a program. They are created after declaration and deleted when the program’s execution stops. They behave like a shared memory between various functions.
Haven’t found what you were looking for? Contact Us
Free Resources