C++ course menu
C++ Data Types: int, double, char, bool, string and Their Sizes
Learn the C++ fundamental data types: integers (int, long long, unsigned), floating point (float, double), char, bool and std::string. See their sizes and ranges with sizeof, overflow, precision, and how to print decimals and true/false.
On this page
A variable's type decides what values it can hold, how much memory it uses, and which operations make sense. C++ has a small set of built-in types, plus types from the standard library such as std::string.
The most used types#
Sizes and ranges#
Each type occupies a fixed number of bytes. The sizeof operator tells you how many:
#include <iostream>
int main() {
std::cout << "char: " << sizeof(char) << '\n';
std::cout << "bool: " << sizeof(bool) << '\n';
std::cout << "int: " << sizeof(int) << '\n';
std::cout << "long long: " << sizeof(long long) << '\n';
std::cout << "float: " << sizeof(float) << '\n';
std::cout << "double: " << sizeof(double) << '\n';
}char: 1
bool: 1
int: 4
long long: 8
float: 4
double: 8These are the sizes on all common desktop systems today. The C++ standard only guarantees minimum sizes (an int is at least 2 bytes), so embedded systems can differ.
The limits come from <limits>:
#include <iostream>
#include <limits>
int main() {
std::cout << std::numeric_limits<int>::min() << '\n';
std::cout << std::numeric_limits<int>::max() << '\n';
std::cout << std::numeric_limits<long long>::max() << '\n';
}-2147483648
2147483647
9223372036854775807So an int holds roughly ±2.1 billion. For bigger values, such as a world population or a timestamp in milliseconds, use long long.
Integer overflow#
If a calculation goes past a type's limit, it overflows. For signed types like int, this is undefined behaviour, and in practice you usually get a wrong, often negative, result. The fix is to use a type that is big enough before the calculation:
#include <iostream>
int main() {
int seconds_per_year = 365 * 24 * 60 * 60;
long long ms_per_year = 1000LL * seconds_per_year;
std::cout << seconds_per_year << '\n';
std::cout << ms_per_year << '\n';
}31536000
31536000000The LL suffix makes 1000 a long long, so the multiplication happens in 64 bits. Writing 1000 * seconds_per_year would multiply two ints and overflow before the result is stored.
unsigned integers#
unsigned int holds only zero and positive numbers, roughly 0 to 4.29 billion. Sizes from the standard library, such as the length of a string, are unsigned (std::size_t). Mixing signed and unsigned values causes surprising results, because a negative number turns into a huge positive one. Prefer plain int (or long long) for ordinary counting and arithmetic.
Floating point: double and float#
double stores decimals with about 15–16 significant digits; float has about 7. Use double by default.
std::cout shows at most 6 significant digits unless you ask for more. <iomanip> controls the format:
#include <iomanip>
#include <iostream>
int main() {
double pi = 3.14159265358979;
std::cout << pi << '\n';
std::cout << std::fixed << std::setprecision(2) << pi << '\n';
std::cout << std::setprecision(10) << pi << '\n';
}3.14159
3.14
3.1415926536std::fixed with std::setprecision(n) means "exactly n digits after the decimal point". It stays in effect for later output until you change it. In C++20 and later you can also use std::format("{:.2f}", pi) from <format>.
Decimal fractions are stored in binary, so most cannot be represented exactly:
#include <iomanip>
#include <iostream>
int main() {
double sum = 0.1 + 0.2;
std::cout << std::setprecision(17) << sum << '\n';
std::cout << (sum == 0.3) << '\n';
}0.30000000000000004
0Never compare floating-point results with ==. Check whether they are close instead, for example std::abs(a - b) < 1e-9. For money, many programs store whole paise or cents in a long long.
char: a single character#
A char holds one character in single quotes. Underneath, it is a small integer (its character code), so you can do arithmetic with it:
#include <iostream>
int main() {
char letter = 'A';
char next = letter + 1;
std::cout << letter << ' ' << next << '\n';
std::cout << static_cast<int>(letter) << '\n';
}A B
65static_cast<int> converts the character to its numeric code, which is 65 for 'A'. You will learn about casts in Type Conversion.
bool: true and false#
A bool is the result of comparisons. It prints as 1 or 0, unless you use std::boolalpha:
#include <iostream>
int main() {
bool is_adult = 21 >= 18;
std::cout << is_adult << '\n';
std::cout << std::boolalpha << is_adult << ' ' << (3 > 5) << '\n';
}1
true falsestd::string: text#
std::string (from <string>) holds text of any length and knows its own size:
#include <iostream>
#include <string>
int main() {
std::string city = "Bengaluru";
std::cout << city << " has " << city.size() << " letters\n";
}Bengaluru has 9 lettersThe Strings lesson covers strings in detail.
Common mistakes#
Practice#
Practice exercise
A bank account has a balance of 2500.756 rupees. Print it with exactly 2 decimal places, then print whether the balance is above 1000 as true or false.
Show solution
#include <iomanip>
#include <iostream>
int main() {
double balance = 2500.756;
std::cout << std::fixed << std::setprecision(2) << balance << '\n';
std::cout << std::boolalpha << (balance > 1000) << '\n';
}2500.76
trueKey takeaway#
Finished this lesson?