
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Variables, their types, and Scope in C++
Each variable in C++ has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable. We declare a variable using the notation −
type variableName;
The type can be primitive(int, bool, float, char, etc.), an enumerated type, a pointer to another type or a more complex type defined by either a data structure or class.
Variable Scope
A scope is a region of the program and broadly speaking there are three places, where variables can be declared −
- Inside a function or a block which is called local variables,
- In the definition of function parameters which is called formal parameters.
- Outside of all functions which are called global variables.
Local variables can be used only by statements that are inside that function or block of code. Local variables are not known to functions on their own.
Example
#include <iostream> using namespace std; int main () { // Local variable declaration: int a, b; int c; // actual initialization a = 10; b = 20; c = a + b; cout << c; return 0; }
Output
This will give the output −
30
Global variables are defined outside of all the functions, usually on top of the program. The global variables will hold their value throughout the lifetime of your program. A global variable can be accessed by any function.
Example
#include <iostream> using namespace std; // Global variable declaration: int g; int main () { // Local variable declaration: int a, b; // actual initialization a = 10; b = 20; g = a + b; cout << g; return 0; }
output
This will give the output −
30
A program can have the same name for local and global variables but the value of a local variable inside a function will take preference. For accessing the global variable with same rame, you'll have to use the scope resolution operator.
Example
#include <iostream> using namespace std; // Global variable declaration: int g = 20; int main () { // Local variable declaration: int g = 10; cout << g; // Local cout << ::g; // Global return 0; }
output
This will give the output −
10 20
- Related Articles
- Scope of Variables in Python
- Scope of Variables in C++
- Scope of Variables in C#
- Global Scope Variables in Postman?
- Scope and lifetime of variables in Java?\n
- C++ Scope of Variables
- What are Local Scope Variables in Postman?
- What are variables and types of variables in C++?
- Operators, Types and Variables in C#
- What is the scope of variables in JavaScript
- Java variables and data types
- Access variables from parent scope in anonymous PHP function
- What is the scope of local variables in Java?
- Internet Cookies and Their Types
- C++ static member variables and their initialization
