Local vs. global variables in C++

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.

Pictorial representation of local vs. global variables’ accessibility
Pictorial representation of local vs. global variables’ accessibility

Local variables

In C++, the variables defined inside the body of a function or a block of code are called local variables of that function or block. These variables can be accessed only inside that function/block.

#include <iostream>
void func(){
int localV = 10;
std::cout<<localV;
}
Local variable example

Global variables

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;
}
Global variable example

Local vs. Global Variables

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.

Code example for local variables

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 access
cout<<localV <<endl;
}
int main(){
func1();
// Illegal to access (Compile time error)
cout<<localV <<endl;
return 0;
}

Explanations

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

Code example for global variables

Here’s the coding example for global variables:

#include <iostream>
using namespace std;
int globarV = 100;
void func1(int v){
// Legal to access
globarV = v;
cout<<"func1: " <<globarV <<endl;
}
int main(){
// Legal to access
cout<<"main: " <<globarV <<endl;
func1(-1);
return 0;
}

Explanations

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

Conclusion

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.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


Can local and global variables have the same name in C++?

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.


Do local variables override global variables in C?

Local variables do not “override” global variables but shadow them within the local scope, meaning the local variable will be used instead of the global one in that context.


Should we avoid global variables in C?

Yes, it is generally recommended to avoid global variables in C to prevent unintended side effects, facilitate debugging, and improve code readability and modularity.


When would we use a global variable instead of a local variable?

You would use a global variable when you need to share data across multiple functions or parts of the program, and maintaining the variable’s value throughout the program’s execution is necessary.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved