[See also type for type system overview and the list of type-related utilities that are provided by the C++ library]
Contents
- 1 Void type
- 2 std::nullptr_t
- 3 Data models
- 4 Signed and unsigned integer types
- 4.1 Modifiers
- 4.2 Properties
- 5 Boolean type
- 6 Character types
- 7 Floating-point types
- 7.1 Properties
- 8 Range of values
- 9 Notes
- 10 Keywords
- 11 Defect reports
- 12 See also
[edit] Void type
void - type with an empty set of values. It is an incomplete type that cannot be completed [consequently, objects of typevoid
are disallowed]. There are no arrays of void
, nor references to void
. However, pointers to void
and
functions returning type void
[procedures in other languages] are permitted. [edit] std::nullptr_t
typedef decltype[nullptr] nullptr_t; | [since C++11] | |
std::nullptr_t is the type of the null pointer literal, nullptr
. It is a distinct type that is not itself a pointer type or a pointer to member type. Its values are null pointer constant [see
NULL], and may be implicitly converted to any pointer and pointer to member type.
sizeof[std::nullptr_t] is equal to sizeof[void *].
[edit] Data models
The choices made by each implementation about the sizes of the fundamental types are collectively known as data model. Four data models found wide acceptance:
32 bit systems:
- LP32 or 2/4/4 [
int
is 16-bit,long
and pointer are 32-bit]
- Win16 API
- ILP32 or 4/4/4 [
int
,long
, and pointer are 32-bit];
- Win32 API
- Unix and Unix-like systems [Linux, macOS]
64 bit systems:
- LLP64 or 4/4/8 [
int
andlong
are 32-bit, pointer is 64-bit]
- Win64 API
- LP64 or 4/8/8 [
int
is 32-bit,long
and pointer are 64-bit]
- Unix and Unix-like systems [Linux, macOS]
Other models are very rare. For example, ILP64 [8/8/8: int
, long
,
and pointer are 64-bit] only appeared in some early 64-bit Unix systems [e.g. UNICOS on Cray].
[edit] Signed and unsigned integer types
int - basic integer type. The keywordint
may be omitted if any of the modifiers listed below are used. If no length modifiers are present, it's guaranteed to have a width of at least 16 bits. However, on 32/64 bit systems it is almost exclusively guaranteed to have width of at least 32 bits [see below]. [edit] Modifiers
Modifies the basic integer type. Can be mixed in any order. Only one of each group can be present in type name.
Signedness
signed - target type will have signed representation [this is the default if omitted] unsigned - target type will have unsigned representationSize
short - target type will be optimized for space and will have width of at least 16 bits. long - target type will have width of at least 32 bits.long long - target type will have width of at least 64 bits. | [since C++11] |
Note: as with all type specifiers, any order is permitted: unsigned long long int and long int unsigned long name the same type.
[edit] Properties
The following table summarizes all available integer types and their properties in various common data models:
signed char | signed char | at least 8 | 8 | 8 | 8 | 8 |
unsigned char | unsigned char | |||||
short | short int | at least 16 | 16 | 16 | 16 | 16 |
short int | ||||||
signed short | ||||||
signed short int | ||||||
unsigned short | unsigned short int | |||||
unsigned short int | ||||||
int | int | at least 16 | 16 | 32 | 32 | 32 |
signed | ||||||
signed int | ||||||
unsigned | unsigned int | |||||
unsigned int | ||||||
long | long int | at least 32 | 32 | 32 | 32 | 64 |
long int | ||||||
signed long | ||||||
signed long int | ||||||
unsigned long | unsigned long int | |||||
unsigned long int | ||||||
long long | long long int [C++11] | at least 64 | 64 | 64 | 64 | 64 |
long long int | ||||||
signed long long | ||||||
signed long long int | ||||||
unsigned long long | unsigned long long int [C++11] | |||||
unsigned long long int |
Note: integer arithmetic is defined differently for the signed and unsigned integer types. See arithmetic operators, in particular integer overflows.
std::size_t is the unsigned integer type of the result of the sizeof
operator as well as the sizeof...
operator and the
alignof
operator [since C++11].
[edit] Boolean type
bool - type, capable of holding one of the two values:true
or false
. The value of sizeof[bool] is implementation defined and might differ from 1. [edit] Character types
signed char - type for signed character representation. unsigned char - type for unsigned character representation. Also used to inspect object representations [raw memory]. char - type for character representation which can be most efficiently processed on the target system [has the same representation and alignment as either signed char or unsigned char, but is always a distinct type]. Multibyte characters strings use this type to represent code units. For every value of type unsigned char in range [0, 255], converting the value to char and then back to unsigned char produces the original value. [since C++11] The signedness of char depends on the compiler and the target platform: the defaults for ARM and PowerPC are typically unsigned, the defaults for x86 and x64 are typically signed. wchar_t - type for wide character representation [see wide strings]. It has the same size, signedness, and alignment as one of the integer types, but is a distinct type. In practice, it is 32 bits and holds UTF-32 on Linux and many other non-Windows systems, but 16 bits and holds UTF-16 code units on Windows. The standard used to require wchar_t to be large enough to represent any supported character code point. However, such requirement cannot be fulfilled on Windows, and thus it is considered as a defect and removed.char16_t - type for UTF-16 character representation, required to be large enough to represent any UTF-16 code unit [16 bits]. It has the same size, signedness, and alignment as std::uint_least16_t, but is a distinct type. char32_t - type for UTF-32 character representation, required to be large enough to represent any UTF-32 code unit [32 bits]. It has the same size, signedness, and alignment as std::uint_least32_t, but is a distinct type. | [since C++11] |
char8_t - type for UTF-8 character representation, required to be large enough to represent any UTF-8 code unit [8 bits]. It has the same size, signedness, and alignment as unsigned char [and therefore, the same size and alignment as char and signed char], but is a distinct type. | [since C++20] |
Besides the minimal bit counts, the C++ Standard guarantees that
1 == sizeof[char]