In C++, a variable is a named storage location that can hold a value of a specific data type. There are several basic data types in C++, including:
- int: This data type is used to store integers (whole numbers) such as -1, 0, and 1.
int x = 5;
- float: This data type is used to store decimal numbers with single precision.
float y = 3.14;
- double: This data type is used to store decimal numbers with double precision.
double z = 3.14159265358979323846;
- char: This data type is used to store individual characters, such as ‘a’, ‘b’, ‘c’, etc.
char letter = 'A';
- bool: This data type is used to store true or false values.
bool isTrue = true;
- string: This data type is used to store strings of characters.
string name = "John";
- Array: This data type is used to store a collection of elements of the same data type.
int myArray[5] = {1, 2, 3, 4, 5};
- Pointers: This data type is used to store memory addresses, and allows you to manipulate memory directly.
int x = 5;
int *p = &x;
- Enumeration: This data type is used to define a set of named integer constants.
enum Color { RED, GREEN, BLUE };
- Structs: This data type is used to group together multiple variables of different data types into one unit.
struct Person {
string name;
int age;
char gender;
};