C++ Basic Data Types
As explained in the Variables chapter, a variable in C++ must be a specified data type:
Example
#include <iostream>
#include <string>
using namespace std;
int main () {
// Creating variables
int myNum = 7; // Integer (whole number)
float myFloatNum = 5.77; // Floating point number
double myDoubleNum = 9.99; // Floating point number
char myLetter = 'A'; // Character
bool myBoolean = true; // Boolean
string myString = "Hello"; // String
// Print variable values
cout << "int: " << myNum << "\n";
cout << "float: " << myFloatNum << "\n";
cout << "double: " << myDoubleNum << "\n";
cout << "char: " << myLetter << "\n";
cout << "bool: " << myBoolean << "\n";
cout << "string: " << myString << "\n";
return 0;
}
Output—->
int: 7
float: 5.77
double: 9.99
char: A
bool: 1
string: Hello
Basic Data Types
The data type specifies the size and type of information the variable will store:
Data Type | Size | Description |
---|---|---|
int | 4 bytes | Stores whole numbers, without decimals |
float | 4 bytes | Stores fractional numbers, containing one or more decimals. Sufficient for storing 7 decimal digits |
double | 8 bytes | Stores fractional numbers, containing one or more decimals. Sufficient for storing 15 decimal digits |
boolean | 1 byte | Stores true or false values |
char | 1 byte | Stores a single character/letter/number, or ASCII values |
You will learn more about the individual data types in the next chapters.