C++ Data Types

C++ Data Types

As explained in the Variables chapter, a variable in C++ must be a specified data type:


int myNum = 5;               // Integer (whole number)
float myFloatNum = 5.99;     // Floating point number
double myDoubleNum = 9.98;   // Floating point number
char myLetter = 'D';         // Character
bool myBoolean = true;       // Boolean
string myText = "Hello";     // String
Run example »

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

Use int when you need to store a whole number without decimals, like 35 or 1000, and float or double when you need a floating point number (with decimals), like 9.99 or 3.14515.


int myNum = 1000;
cout << myNum;
Run example »


float myNum = 5.75;
cout << myNum;
Run example »


double myNum = 19.99;
cout << myNum;
Run example »

float vs. double

The precision of a floating point value indicates how many digits the value can have after the decimal point. The precision of float is only six or seven decimal digits, while double variables have a precision of about 15 digits. Therefore it is safer to use double for most calculations.

Scientific Numbers

A floating point number can also be a scientific number with an "e" to indicate the power of 10:


float f1 = 35e3;
double d1 = 12E4;
cout << f1;
cout << d1;
Run example »


A boolean data type is declared with the bool keyword and can only take the values true or false. When the value is returned, true = 1 and false = 0.


bool isCodingFun = true;
bool isFishTasty = false;
cout << isCodingFun;  // Outputs 1 (true)
cout << isFishTasty;  // Outputs 0 (false)
Run example »

Boolean values are mostly used for conditional testing, which you will learn more about in a later chapter.


The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c':


char myGrade = 'B';
cout << myGrade;
Run example »

Alternatively, you can use ASCII values to display certain characters:


char a = 65, b = 66, c = 67;
cout << a;
cout << b;
cout << c;
Run example »

Tip: A list of all ASCII values can be found in our ASCII Table Reference.


The string type is used to store a sequence of characters (text). This is not a built-in type, but it behaves like one in its most basic usage. String values must be surrounded by double quotes:


string greeting = "Hello";
cout << greeting;

To use strings, you must include an additional header file in the source code, the <string> library:


// Include the string library
#include <string>

// Create a string variable
string greeting = "Hello";

// Output string value
cout << greeting;
Run example »

You will learn more about strings, in our C++ Strings Chapter.

C++ Exercises

Test Yourself With Exercises


Add the correct data type for the following variables:

 myNum = 9;
 myDoubleNum = 8.99;
 myLetter = 'A';
 myBool = false;
 myText = "Hello World";

Start the Exercise