C++ Variables are the names given to memory locations. It is used to store any data type value. We can say that variables are the containers to store any value (information) within the program.
For example int age =27; where age is an integer type variable while float area= 16.8; is a floating-point variable.
How to declare a variable
Here is the syntax to declare a variable in C++
data_type variable_name = value;
Variables are of different types depending upon the data type.
Variable Type | Description | Examples |
int | Store non-decimal whole numbers | 190, -190 |
float | Store decimal numbers up to 7 decimal places | 5.9, -5.9 |
double | Used to store decimal numbers up to 15 decimal places | 45.7567, -45.7567 |
char | stores only a single character(alphabet or a number) | A, b, 6 |
bool | Used to store true or false value (T/F) | True, False |
string | It stores plain text | Hello World |
More Examples: C++ Variables
int marks =90;
float speed = 98.46;
double coordinates = 45.8979;
bool graduated = true;
char value = ‘A’;
string message = “Hello World”
We can declare multiple C++ variables of the same type in a single line by using the “,” separator.
Here is the example
int a=4, b=8, c=9;