Variables are how you store data in a program. Think of a variable like a labeled box. You put a value in it, and later you can look inside the box to get that value back. In C++, you have to tell the compiler what kind of data each box will hold before you can use it.
Declaring Variables
To declare a variable, write the type first, then the name, then an optional value.
The basic types are int for whole numbers, double for
decimal numbers, char for single characters, and bool
for true/false values.
#include <iostream>
using namespace std;
int main() {
int age = 25;
double price = 19.99;
char grade = 'A';
bool isReady = true;
cout << age;
return 0;
}
Variable Naming Rules
Names can contain letters, digits, and underscores. They cannot start with a digit.
They cannot be reserved keywords like int or return.
And they are case-sensitive โ age and Age are different variables.
Use meaningful names. studentCount is better than sc.
Multiple Variables
You can declare multiple variables of the same type in one line by separating them with commas. Just be careful โ it is easy to forget to initialize one.
#include <iostream>
using namespace std;
int main() {
int x = 5, y = 10, z = 15;
cout << "x is " << x << ", y is " << y << ", z is " << z;
return 0;
}