
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Local and Global Variables in C++
In C++, the scope defines the region within which a variable can be accessed. So, if broadly speaking, there are three main places where variables can be declared and accessed:
- Inside a function or a block, which are called local variables.
- In the definition of function parameters, which are called formal parameters.
- Outside of all functions, which are called global variables.
Local Variables
Local variables are the variables that are defined inside a function, method, or block of code within curly braces {}. These variables cannot be accessed or used outside of that function or block.
Example
Here is the following example code, showcasing the local variables inside a function. where these variables will be accessible in their function or scope only and can't be accessed out of its scope:
#include <iostream> using namespace std; void calculateSum() { int x = 15; // local variable declaration int y = 25; // local variable declaration int sum = x + y; cout << "Sum is: " << sum << endl; } int main() { calculateSum(); // calling a function // cout << x; this will be wrong because the x variable is not directly accessible here out of its local function scope return 0; }
Output
Sum is: 40
Example
Here is the following example code, showcasing the Local variables inside a block. where these variables will be accessible till their block or scope only:
#include <iostream> using namespace std; int main() { int a = 5; // this is local variable to main() function if (a > 0) { int b = 10; // local variable to this if-block cout << "Inside block: a = " << a << ", b = " << b << endl; } cout << a <<endl; // cout << b; but b is not accessible outside the if-block return 0; }
Output
Inside block: a = 5, b = 10 5
Global Variables
These are the variables, which are defined outside of all the functions, usually at the top of the program. These global variables hold their values throughout the lifetime of your program and can be accessed by any function or block.
Example
Here is the following example showcasing how the global variable is accessed and used in the main() function:
#include <iostream> using namespace std; // Global variable declaration: int g; int main () { // Local variable declaration: int a, b; // initialization of local variables a = 10; b = 20; g = a + b; // using local variables to update global variable cout << g; return 0; }
Output
30
Variable Shadowing
In C++, when both global and local variables have the same name, then the local variable takes precedence within its scope. This means the value of the local variable will be used within that scope. In case if you want to access a global variable, you need to use the scope resolution operator (::).
Example
Here is the following example showcasing how, by default, a local variable is used when both local and global variables have the same name. So, to access the global variable, we used the scope resolution operator.
#include <iostream> using namespace std; // Global variable declaration: int g = 20; int main () { // Local variable declaration: int g = 10; cout << g << endl; // it will call local variable cout << ::g << endl; // using the scope resolution operator will give you access to call the value of the global variable return 0; }
Output
10 20