C++ Variables

Variable in c++ 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 TypeDescriptionExamples
intStore non-decimal whole numbers190, -190
floatStore decimal numbers up to 7 decimal places5.9, -5.9
doubleUsed to store decimal numbers up to 15 decimal places45.7567, -45.7567
charstores only a single character(alphabet or a number)A, b, 6
boolUsed to store true or false value (T/F)True, False
stringIt stores plain textHello World

More Examples

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 variables of the same type in a single line by using the “,” separator.

Here is the example

int a=4, b=8, c=9;

Leave a Reply