Variables in C++
1. What is a Variable?
A variable is a named memory location used to store data in a C++ program.
The value stored in a variable can usually be changed while the program is running.
For example:
int age = 20;
Here:
int→ data typeage→ variable name20→ value stored in the variable=→ assignment operator
2. Syntax of a Variable
The general syntax for declaring a variable is:
data_type variable_name;
Example:
int age; float salary; char grade;
A variable can also be declared and initialized at the same time:
int age = 20; float salary = 25000.50; char grade = 'A';
3. Declaration and Initialization
Declaration
Declaration tells the compiler about the variable and its data type.
int age;
Initialization
Initialization means giving a value to a variable.
int age = 20;
You can also declare first and assign a value later:
int age; age = 20;
4. Example of Variables
#include <iostream> using namespace std; int main() { int age = 20; float height = 5.4; char grade = 'A'; cout << "Age: " << age << endl; cout << "Height: " << height << endl; cout << "Grade: " << grade << endl; return 0; }
Output
Age: 20 Height: 5.4 Grade: A
5. Types of Variables
Variables can be classified based on where they are declared and how long they exist.
5.1 Local Variable
A local variable is declared inside a function or block.
#include <iostream> using namespace std; int main() { int age = 20; cout << age; return 0; }
Here, age is a local variable.
5.2 Global Variable
A global variable is declared outside all functions.
#include <iostream> using namespace std; int age = 20; int main() { cout << age; return 0; }
Here, age is a global variable and can be accessed by functions in its scope.
5.3 Static Variable
A static local variable keeps its value between function calls.
#include <iostream> using namespace std; void count() { static int x = 0; x++; cout << x << endl; } int main() { count(); count(); count(); return 0; }
Output
1 2 3
The variable x is not destroyed after the first function call.
5.4 Constant Variable
A constant variable cannot be changed after initialization.
The const keyword is used.
const int DAYS = 7;
This is invalid:
DAYS = 10;
because DAYS is constant.
Example:
#include <iostream> using namespace std; int main() { const float PI = 3.14159; cout << PI; return 0; }
6. Variable Naming Rules
C++ has specific rules for naming variables.
Rules
- A variable name can contain letters, digits, and underscores.
- It cannot start with a digit.
- It cannot contain spaces.
- It cannot be a C++ keyword.
- C++ variable names are case-sensitive.
- Special symbols such as
@,#, and$should not be used.
Valid Names
age studentName total_marks marks1 _myVariable
Invalid Names
1age // Cannot start with a digit student name // Space is not allowed total-marks // '-' is not allowed class // C++ keyword
7. Case Sensitivity
C++ is case-sensitive.
Therefore, these are different variables:
int age = 20; int Age = 25; int AGE = 30;
age, Age, and AGE are treated as three different identifiers.
8. Multiple Variables
Multiple variables of the same data type can be declared in one statement.
int a = 10, b = 20, c = 30;
Example:
#include <iostream> using namespace std; int main() { int a = 10, b = 20, c = 30; cout << a << endl; cout << b << endl; cout << c << endl; return 0; }
9. Changing the Value of a Variable
The value of a normal variable can be changed.
int age = 20; age = 21;
Example:
#include <iostream> using namespace std; int main() { int number = 10; cout << number << endl; number = 50; cout << number << endl; return 0; }
Output
10 50
10. Variable Assignment
The assignment operator = is used to assign a value.
int x; x = 100;
The right-hand value is assigned to the variable on the left.
x = 100 ↓ ↓ Variable Value
11. Variables with Different Data Types
Different types of variables can store different kinds of data.
int age = 20; float price = 99.50; double salary = 50000.75; char grade = 'A'; bool passed = true;
Example:
#include <iostream> using namespace std; int main() { int age = 20; float price = 99.5; char grade = 'A'; bool passed = true; cout << "Age: " << age << endl; cout << "Price: " << price << endl; cout << "Grade: " << grade << endl; cout << "Passed: " << passed << endl; return 0; }
12. Taking Input into Variables
The cin object is used to take input from the user.
#include <iostream> using namespace std; int main() { int age; cout << "Enter your age: "; cin >> age; cout << "Your age is: " << age; return 0; }
Example Output
Enter your age: 21 Your age is: 21
13. Variable Scope
Scope refers to the part of a program where a variable can be accessed.
Example:
#include <iostream> using namespace std; int main() { int x = 10; { int y = 20; cout << x << endl; cout << y << endl; } cout << x << endl; return 0; }
Here:
xcan be accessed throughout themain()function.ycan only be accessed inside its block.
14. auto Variables
C++ can automatically determine a variable's type using auto.
auto age = 20; auto price = 99.50; auto grade = 'A';
The compiler determines the appropriate type from the initial value.
Example:
#include <iostream> using namespace std; int main() { auto age = 20; auto price = 99.50; cout << age << endl; cout << price << endl; return 0; }
15. Best Practices for Variables
Good variable names make programs easier to understand.
Good
int studentAge; float productPrice; int totalMarks;
Poor
int x; float a; int z;
Use meaningful names whenever possible.
For constants, uppercase names are commonly used:
const int MAX_SIZE = 100; const double PI = 3.14159;
16. Important Points
- A variable stores data in memory.
- Every variable should have a data type.
- Variables can be declared and initialized.
- Normal variables can have their values changed.
constvariables cannot be modified after initialization.- Variable names are case-sensitive.
- Variables have a specific scope.
cincan be used to take input into variables.autoallows the compiler to deduce a variable's type.
Conclusion
Variables are one of the most fundamental concepts in C++. They allow programs to store, access, and modify data. Understanding variable declaration, initialization, naming rules, scope, constants, and input is essential before moving on to more advanced C++ concepts.