In C++, there are several basic variable types that you can use to store and manipulate different kinds of data. Here are some of the commonly used variable types:
Integers ('int')
Integers are used to store whole numbers without decimal points. They can be positive, negative, or zero. For example:
int age = 25; int quantity = -10;
Floating point numbers ('float' and 'double')
Floating-point numbers are used to store decimal numbers. They can represent both small and large numbers with decimal precision. float represents single-precision floating-point numbers, while double represents double-precision floating-point numbers. For example:
float pi = 3.14; double salary = 2500.50;
Characters ('char')
Characters are used to store individual characters. They are enclosed in single quotes (' '). For example:
char grade = 'A'; char symbol = '*';
Booleans ('bool')
Booleans can store either true or false. They are commonly used for logical operations and decision-making. For example:
bool isPassed = true; bool isRaining = false;
Strings ('std::string')
Strings are used to store sequences of characters. They are represented using the std::string class from the <string> library. For example:
#include <string> std::string name = "John Doe";