In C++, a variable is like a container or box that stores data which can change during the program execution. Every variable has a name and a data type that defines the kind of value it can hold.
To declare a variable, you write its data type followed by the variable name:
int age; float price; char grade;
You can assign a value to a variable right when you declare it, which is called initialization:
int age = 20; float price = 99.99; char grade = 'B';
You can also declare variables first and assign values later in the program:
int age; // declaration age = 25; // assignment
int
, return
, for
).age
and Age
are different).#include <iostream> using namespace std; int main() { int age = 21; // declare and initialize float height; // declare height = 5.9; // assign value later cout << "Age: " << age << endl; cout << "Height: " << height << " feet" << endl; return 0; }
Variables are essential to store and manipulate data in a program. You must declare variables with a specific data type and follow naming rules to create valid variables in C++.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!