Variables are named storage locations used to hold data during program execution. Each variable has a data type that determines the kind of value it can store.
- Variables must be declared before they are used.
- Their values can be modified during execution unless they are declared as const.
#include <iostream>
using namespace std;
int main() {
// Create and initialize a variable
int num = 3;
// Access the variable
cout << num << endl;
// Update the value
num = 7;
cout << num;
return 0;
}
Output
3 7
Explanation: The integer variable num is initialized with the value 3. The assignment operator (=) updates its value to 7.
Variable Declaration and Initialization
Variable declaration specifies the data type and name, while initialization assigns an initial value when the variable is defined.
Syntax:
data_type variable_name = value;
Here:
- data_type specifies the type of data stored in the variable.
- variable_name represents the variable's identifier.
- value specifies the initial value assigned to the variable.

Multiple variables of the same type can also be defined in a single statement.
int a, b, c;
C++ supports different forms of initialization.
- int a = 10; // Copy initialization
- int b(20); // Direct initialization
- int c{30}; // Direct-list initialization
Assigning a value after definition is called assignment, not initialization.
int num; // Definition
num = 3; // Assignment
Rules for Naming Variables
Variable names must follow these rules:
- Names can contain letters, digits, underscores (_), and certain Unicode characters.
- Names cannot begin with a digit
- Reserved keywords such as int, class, and if cannot be used.
- Variable names are case-sensitive.
- Spaces and special characters such as @, %, and # are not allowed.
- Names containing double underscores or beginning with an underscore followed by an uppercase letter are reserved for the implementation.
- Names beginning with an underscore are reserved for the implementation in the global namespace.
How are variables used?
Variables can be used in expressions, arithmetic operations, assignments, and function calls.
#include <iostream>
using namespace std;
int main() {
int num1 = 10;
int num2 = 20;
// Assign a variable's value
int value = num1;
// Perform addition
int sum = num1 + num2;
cout << "Value: " << value << endl;
cout << "Sum: " << sum;
return 0;
}
Output
Value: 10 Sum: 30
Memory Management of Variables
Storage allocation and lifetime depend on the variable's data type and storage duration. Compiler optimizations may also affect how variables are stored.
- Local variables of fundamental types should be initialized before their values are read.
- Global and static variables are zero-initialized if no other initialization applies.
- Automatic variables generally exist until their enclosing block ends.
- Static variables exist throughout the program's execution.
- Compilers may store variables in registers or eliminate unnecessary storage through optimization.
Related Article:

