C++ Reference ยท Types and Language Concepts
Fundamental types
Reference for C++ fundamental types: integers, char types, bool, floating point, void and nullptr_t, with sizes, ranges, conversions and pitfalls.
The fundamental types (also called built-in types) are the types the language provides without any library: bool, the character types, the integer types, the floating-point types, void and std::nullptr_t. Every other type, from std::string to your own classes, is built from them.
The standard fixes only minimum guarantees and relationships, not exact sizes. sizeof(char) is always 1, and short is at least 16 bits, int at least 16, long at least 32 and long long at least 64, with each type at least as wide as the one before. On typical 64-bit desktop platforms int is 32 bits; long is 64 bits on Linux and macOS but 32 on Windows. When you need an exact width, use the types from <cstdint>.
int count = 0; // typically 32 bits double ratio = 0.5; // IEEE 754 binary64 on virtually all platforms char letter = 'A'; // 1 byte, holds a character code bool done = false;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
The types at a glance
| Category | Types | Notes |
|---|---|---|
| Boolean | bool |
true or false; size is at least 1 byte (usually exactly 1). |
| Character | char, signed char, unsigned char, wchar_t, char8_t (C++20), char16_t, char32_t |
char is a distinct type whose signedness is implementation-defined. char8_t holds UTF-8 code units, char16_t/char32_t UTF-16/UTF-32. |
| Signed integer | short, int, long, long long |
Each may be written with signed and int omitted: unsigned long long. |
| Unsigned integer | unsigned short, unsigned, unsigned long, unsigned long long |
Arithmetic wraps modulo 2^N. |
| Floating point | float, double, long double |
Usually IEEE 754 single, double and (on x86) 80-bit extended precision. C++23 adds optional std::float32_t and friends. |
| Void | void |
No values; used for "returns nothing" and void*. |
| Null pointer | std::nullptr_t |
The type of nullptr. |
Errors / undefined behavior: signed integer overflow, division by zero (integer), shifting by a negative amount or by at least the width of the type, and reading an uninitialized local are undefined behavior. Converting an out-of-range floating-point value to an integer is also undefined.
Usage notes
- Default to
intanddoublefor everyday values; usestd::size_tfor sizes and indexes, and fixed-width types for file formats and protocols. - Prefer signed types for arithmetic and avoid mixing signed with unsigned:
-1 < 1uisfalsebecause-1converts to a huge unsigned value. - Integral promotion. Types narrower than
intare promoted tointin expressions, sochar + charis anint. That is why printing needs+cor a cast. charsignedness differs between platforms (signed on x86 Linux, unsigned on many ARM systems). Useunsigned charorstd::bytefor raw bytes.- Do not compare floats with
==after arithmetic; compare with a tolerance. RememberNaN != NaN. - Initialize always.
int x;inside a function holds an indeterminate value;int x{};gives 0. - Brace initialization rejects narrowing conversions:
int n{3.5};is a compile error. sizeof(int)is not a guarantee of 4. If you assume it, addstatic_assert(sizeof(int) == 4);.