Primitive Types

TypeTypical Size
bool8 bits
char8 bits
short16 bits
int32 bits
long32 bits
long long64 bits

The standard only stipulates that sizeof(char) == 1 and sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long).

Except for bool, these types can be signed or unsigned. The standard does not specify the size of bool.


char c; // [-2^7 to 2^7 - 1]
unsigned char c2; // [0 to 2^8]

We also have floating-point types: float, double and long double. The standard only requires that double provides at least as much precision as float and that long double provides at least as much as double. On x86 systems, float is typically 32 bits, double is typically 64, and long double is commonly 80 or 128.

C++ also has literal type specifiers to convert a literal value to a certain type. A literal is something that's already a value and cannot be assigned such as 42 or 0.0.


auto f = 0.1f; // f is a float
auto d = 0.1; // d is double
auto u = 10u; // unsigned int
auto lng = 20l; // long
auto ulng = 20ul; // unsigned long
auto ullng = 200ull; // unsigned long long
auto i = 200; // i is int
auto ch = 'H'; // char

Finally, we can also input numbers in different bases such as hex, octal or binary and use the single quote ' as a digit separator to make things more readable.

auto h = 0xA; //h  is the integer 10
auto oct = 024; //oct is the integer 14
auto bin = 0b11; // bin is the integer 3

auto pi = 3.14159'26535'89793'23846'26433'83279'50288;

Hex and binary are pretty important in systems programming so make sure you read up on that if you're unfamiliar.

Implicit Type Conversions

Implicitly, signed values become unsigned when you mix the two. Thus, we never want to have signed/unsigned mismatches and that's one thing auto helps prevent.

const auto num = 10;
auto n2 = num - 20u; // 2^32 - 10

The following type conversions are implicit, where a type can be converted to any type to the right of it:

char -> short -> int -> long -> long long -> float -> double.

bool can be implicitly converted to any integral type. true becomes 1 and false becomes 0. Integral types convert to bool where 0 is false and everything else is true.

If a value goes out of range on an unsigned integral type, the value wraps back around from the lowest point in that type's range. Out of range on signed types is undefined.

Further Reading

C++ Primer 2.1 - 2.1.3