Difference between revisions of "cpp/language/types"
(The table is only about standard integer types, not all the integer types) |
(Removed the keyword revboxes.) |
||
(11 intermediate revisions by 4 users not shown) | |||
Line 4: | Line 4: | ||
{{petty|(See also {{rlp|type}} for type system overview and [[cpp/types|the list of type-related utilities]] that are provided by the C++ library)}} | {{petty|(See also {{rlp|type}} for type system overview and [[cpp/types|the list of type-related utilities]] that are provided by the C++ library)}} | ||
− | === | + | The following types are collectively called ''fundamental types''{{sep}}: |
+ | * (possibly cv-qualified) {{c/core|void}} | ||
+ | {{rrev|since=c++11| | ||
+ | * (possibly cv-qualified) {{lc|std::nullptr_t}} | ||
+ | }} | ||
+ | * [[#Integral types|integral types]] | ||
+ | * [[#Floating-point types|floating-point types]] | ||
+ | |||
+ | {{anchor|void}} | ||
+ | ==={{c/core|void}}=== | ||
:{{c/core|void}} — type with an empty set of values. It is an {{rlp|incomplete type}} that cannot be completed (consequently, objects of type {{c/core|void}} are disallowed). There are no {{rlp|array}}s of {{c/core|void}}, nor {{rlp|reference}}s to {{c/core|void}}. However, {{rlp|pointer#Pointers to void|pointers to {{c/core|void}}}} and {{rlp|function}}s returning type {{c/core|void}} (''procedures'' in other languages) are permitted. | :{{c/core|void}} — type with an empty set of values. It is an {{rlp|incomplete type}} that cannot be completed (consequently, objects of type {{c/core|void}} are disallowed). There are no {{rlp|array}}s of {{c/core|void}}, nor {{rlp|reference}}s to {{c/core|void}}. However, {{rlp|pointer#Pointers to void|pointers to {{c/core|void}}}} and {{rlp|function}}s returning type {{c/core|void}} (''procedures'' in other languages) are permitted. | ||
+ | {{rrev|since=c++11| | ||
{{anchor|nullptr_t}} | {{anchor|nullptr_t}} | ||
− | ==={{lc|std::nullptr_t | + | ==={{lc|std::nullptr_t}}=== |
{{dcl begin}} | {{dcl begin}} | ||
{{dcl header|cstddef}} | {{dcl header|cstddef}} | ||
Line 16: | Line 26: | ||
{{dcl end}} | {{dcl end}} | ||
− | {{lc|std::nullptr_t}} is the type of the null pointer literal, {{rlpt|nullptr}}. It is a distinct type that is not itself a pointer type or a pointer to member type. Its | + | {{lc|std::nullptr_t}} is the type of the null pointer literal, {{rlpt|nullptr}}. It is a distinct type that is not itself a pointer type or a pointer to member type. All Its prvalues are {{rlp|pointer#Null pointers|null pointer constants}}. |
{{c|sizeof(std::nullptr_t)}} is equal to {{c|sizeof(void*)}}. | {{c|sizeof(std::nullptr_t)}} is equal to {{c|sizeof(void*)}}. | ||
+ | }} | ||
− | + | {{anchor|Integer types}} | |
− | + | ===Integral types=== | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | === | + | |
====Standard integer types==== | ====Standard integer types==== | ||
:{{c/core|int}} — basic integer type. The keyword {{c/core|int}} 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). | :{{c/core|int}} — basic integer type. The keyword {{c/core|int}} 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). | ||
Line 62: | Line 56: | ||
The following table summarizes all available standard integer types and their properties in various common data models: | The following table summarizes all available standard integer types and their properties in various common data models: | ||
− | {| class="wikitable" style="text-align:center;" | + | {|class="wikitable" style="text-align:center;" |
|- | |- | ||
− | ! rowspan="2" | Type specifier | + | ! rowspan="2"|Type specifier |
− | ! rowspan="2" | Equivalent type | + | ! rowspan="2"|Equivalent type |
− | ! colspan="5" | Width in bits by data model | + | ! colspan="5"|Width in bits by [[#Data models|data model]] |
|- | |- | ||
<!-- --> | <!-- --> | ||
<!-- --> | <!-- --> | ||
− | ! C++ standard | + | ! C++ standard |
! LP32 | ! LP32 | ||
! ILP32 | ! ILP32 | ||
Line 76: | Line 70: | ||
! LP64 | ! LP64 | ||
|- | |- | ||
− | | {{left|{{c/core|signed char}}}} | + | |{{left|{{c/core|signed char}}}} |
− | | {{c/core|signed char}} | + | |{{c/core|signed char}} |
− | | rowspan="2" | at least<br> '''8''' | + | |rowspan="2"|at least<br> '''8''' |
− | | rowspan="2" | '''8''' | + | |rowspan="2"|'''8''' |
− | | rowspan="2" | '''8''' | + | |rowspan="2"|'''8''' |
− | | rowspan="2" | '''8''' | + | |rowspan="2"|'''8''' |
− | | rowspan="2" | '''8''' | + | |rowspan="2"|'''8''' |
|- | |- | ||
− | | {{left|{{c/core|unsigned char}}}} | + | |{{left|{{c/core|unsigned char}}}} |
− | | {{c/core|unsigned char}} | + | |{{c/core|unsigned char}} |
|- | |- | ||
− | | {{left|{{c/core|short}}}} | + | |{{left|{{c/core|short}}}} |
− | | rowspan="4" | {{c/core|short int}} | + | |rowspan="4"|{{c/core|short int}} |
− | | rowspan="6" | at least<br> '''16''' | + | |rowspan="6"|at least<br> '''16''' |
− | | rowspan="6" | '''16''' | + | |rowspan="6"|'''16''' |
− | | rowspan="6" | '''16''' | + | |rowspan="6"|'''16''' |
− | | rowspan="6" | '''16''' | + | |rowspan="6"|'''16''' |
− | | rowspan="6" | '''16''' | + | |rowspan="6"|'''16''' |
|- | |- | ||
− | | {{left|{{c/core|short int}}}} | + | |{{left|{{c/core|short int}}}} |
|- | |- | ||
− | | {{left|{{c/core|signed short}}}} | + | |{{left|{{c/core|signed short}}}} |
|- | |- | ||
− | | {{left|{{c/core|signed short int}}}} | + | |{{left|{{c/core|signed short int}}}} |
|- | |- | ||
− | | {{left|{{c/core|unsigned short}}}} | + | |{{left|{{c/core|unsigned short}}}} |
− | | rowspan="2" | {{c/core|unsigned short int}} | + | |rowspan="2"|{{c/core|unsigned short int}} |
|- | |- | ||
− | | {{left|{{c/core|unsigned short int}}}} | + | |{{left|{{c/core|unsigned short int}}}} |
|- | |- | ||
− | | {{left|{{c/core|int}}}} | + | |{{left|{{c/core|int}}}} |
− | | rowspan="3" | {{c/core|int}} | + | |rowspan="3"|{{c/core|int}} |
− | | rowspan="5" | at least<br> '''16''' | + | |rowspan="5"|at least<br> '''16''' |
− | | rowspan="5" | '''16''' | + | |rowspan="5"|'''16''' |
− | | rowspan="5" | '''32''' | + | |rowspan="5"|'''32''' |
− | | rowspan="5" | '''32''' | + | |rowspan="5"|'''32''' |
− | | rowspan="5" | '''32''' | + | |rowspan="5"|'''32''' |
|- | |- | ||
− | | {{left|{{c/core|signed}}}} | + | |{{left|{{c/core|signed}}}} |
|- | |- | ||
− | | {{left|{{c/core|signed int}}}} | + | |{{left|{{c/core|signed int}}}} |
|- | |- | ||
− | | {{left|{{c/core|unsigned}}}} | + | |{{left|{{c/core|unsigned}}}} |
− | | rowspan="2" | {{c/core|unsigned int}} | + | |rowspan="2"|{{c/core|unsigned int}} |
|- | |- | ||
− | | {{left|{{c/core|unsigned int}}}} | + | |{{left|{{c/core|unsigned int}}}} |
|- | |- | ||
− | | {{left|{{c/core|long}}}} | + | |{{left|{{c/core|long}}}} |
− | | rowspan="4" | {{c/core|long int}} | + | |rowspan="4"|{{c/core|long int}} |
− | | rowspan="6" | at least<br> '''32''' | + | |rowspan="6"|at least<br> '''32''' |
− | | rowspan="6" | '''32''' | + | |rowspan="6"|'''32''' |
− | | rowspan="6" | '''32''' | + | |rowspan="6"|'''32''' |
− | | rowspan="6" | '''32''' | + | |rowspan="6"|'''32''' |
− | | rowspan="6" | '''64''' | + | |rowspan="6"|'''64''' |
|- | |- | ||
− | | {{left|{{c/core|long int}}}} | + | |{{left|{{c/core|long int}}}} |
|- | |- | ||
− | | {{left|{{c/core|signed long}}}} | + | |{{left|{{c/core|signed long}}}} |
|- | |- | ||
− | | {{left|{{c/core|signed long int}}}} | + | |{{left|{{c/core|signed long int}}}} |
|- | |- | ||
− | | {{left|{{c/core|unsigned long}}}} | + | |{{left|{{c/core|unsigned long}}}} |
− | | rowspan="2" | {{c/core|unsigned long int}} | + | |rowspan="2"|{{c/core|unsigned long int}} |
|- | |- | ||
− | | {{left|{{c/core|unsigned long int}}}} | + | |{{left|{{c/core|unsigned long int}}}} |
|- | |- | ||
− | | {{left|{{c/core|long long}}}} | + | |{{left|{{c/core|long long}}}} |
− | | rowspan="4" | {{c/core|long long int}}<br>{{mark c++11}} | + | |rowspan="4"|{{c/core|long long int}}<br>{{mark c++11}} |
− | | rowspan="6" | at least<br> '''64''' | + | |rowspan="6"|at least<br> '''64''' |
− | | rowspan="6" | '''64''' | + | |rowspan="6"|'''64''' |
− | | rowspan="6" | '''64''' | + | |rowspan="6"|'''64''' |
− | | rowspan="6" | '''64''' | + | |rowspan="6"|'''64''' |
− | | rowspan="6" | '''64''' | + | |rowspan="6"|'''64''' |
|- | |- | ||
− | | {{left|{{c/core|long long int}}}} | + | |{{left|{{c/core|long long int}}}} |
|- | |- | ||
− | | {{left|{{c/core|signed long long}}}} | + | |{{left|{{c/core|signed long long}}}} |
|- | |- | ||
− | | {{left|{{c/core|signed long long int}}}} | + | |{{left|{{c/core|signed long long int}}}} |
|- | |- | ||
− | | {{left|{{c/core|unsigned long long}}}} | + | |{{left|{{c/core|unsigned long long}}}} |
− | | rowspan="2" | {{c/core|unsigned long long int}}<br>{{mark c++11}} | + | |rowspan="2"|{{c/core|unsigned long long int}}<br>{{mark c++11}} |
|- | |- | ||
− | | {{left|{{c/core|unsigned long long int}}}} | + | |{{left|{{c/core|unsigned long long int}}}} |
|} | |} | ||
Note: integer arithmetic is defined differently for the signed and unsigned integer types. See {{rlp|operator arithmetic|arithmetic operators}}, in particular {{rlp|operator arithmetic#Overflows|integer overflows}}. | Note: integer arithmetic is defined differently for the signed and unsigned integer types. See {{rlp|operator arithmetic|arithmetic operators}}, in particular {{rlp|operator arithmetic#Overflows|integer overflows}}. | ||
− | {{lc|std::size_t}} is the unsigned integer type of the result of the {{rlpt|sizeof}} operator {{rev inl|since=c++11|as well as the {{rlpt|sizeof...}} operator and the {{rlpt|alignof}} operator}}. | + | {{lc|std::size_t}} is the unsigned integer type of the result of the {{rlpt|sizeof}} operator{{rev inl|since=c++11| as well as the {{rlpt|sizeof...}} operator and the {{rlpt|alignof}} operator}}. |
− | {{ | + | {{rrev|since=c++11| |
− | ====Extended integer types | + | ====Extended integer types==== |
The extended integer types are implementation-defined. Note that [[cpp/types/integer|fixed width integer types]] are typically aliases of the standard integer types. | The extended integer types are implementation-defined. Note that [[cpp/types/integer|fixed width integer types]] are typically aliases of the standard integer types. | ||
+ | }} | ||
− | ===Boolean type=== | + | ====Boolean type==== |
− | :{{c/core|bool}} — type, capable of holding one of the two values: {{rlpt|bool literal|true}} or {{rlpt|bool literal|false}}. The value of {{c|sizeof(bool)}} is implementation defined and might differ from {{c|1}}. | + | :{{c/core|bool}} — integer type, capable of holding one of the two values: {{rlpt|bool literal|true}} or {{rlpt|bool literal|false}}. The value of {{c|sizeof(bool)}} is implementation defined and might differ from {{c|1}}. |
− | ===Character types=== | + | ====Character types==== |
+ | Character types are integer types used for a character representation. | ||
:{{c/core|signed char}} — type for signed character representation. | :{{c/core|signed char}} — type for signed character representation. | ||
:{{c/core|unsigned char}} — type for unsigned character representation. Also used to inspect {{rlp|object|object representations}} (raw memory). | :{{c/core|unsigned char}} — type for unsigned character representation. Also used to inspect {{rlp|object|object representations}} (raw memory). | ||
Line 205: | Line 201: | ||
:* On PowerPC {{enwiki|Quadruple-precision floating-point format#Double-double arithmetic|double-double}} can be used. | :* On PowerPC {{enwiki|Quadruple-precision floating-point format#Double-double arithmetic|double-double}} can be used. | ||
− | {{ | + | {{rrev|since=c++23| |
− | + | ====Extended floating-point types==== | |
− | ====Extended floating-point types | + | |
The extended floating-point types are implementation-defined. They may include [[cpp/types/floating-point|fixed width floating-point types]]. | The extended floating-point types are implementation-defined. They may include [[cpp/types/floating-point|fixed width floating-point types]]. | ||
+ | }} | ||
{{anchor|Floating-point properties}} | {{anchor|Floating-point properties}} | ||
Line 214: | Line 210: | ||
Floating-point types may support [[cpp/types/numeric limits|special values]]: | Floating-point types may support [[cpp/types/numeric limits|special values]]: | ||
* ''infinity'' (positive and negative), see {{lc|INFINITY}} | * ''infinity'' (positive and negative), see {{lc|INFINITY}} | ||
− | * the ''negative zero'', {{c|-0.0}}. It compares equal to the positive zero, but is meaningful in some arithmetic operations, e.g. {{c|1=1.0 / 0.0 == INFINITY}}, but {{c|1=1.0/-0.0 == -INFINITY}}), and for some mathematical functions, e.g. {{ltt|cpp/numeric/complex/sqrt|sqrt{{small|(std::complex)}}}} | + | * the ''negative zero'', {{c|-0.0}}. It compares equal to the positive zero, but is meaningful in some arithmetic operations, e.g. {{c|1=1.0 / 0.0 == INFINITY}}, but {{c|1=1.0 / -0.0 == -INFINITY}}), and for some mathematical functions, e.g. {{ltt|cpp/numeric/complex/sqrt|sqrt{{small|(std::complex)}}}} |
* ''not-a-number'' (NaN), which does not compare equal with anything (including itself). Multiple bit patterns represent NaNs, see {{lc|std::nan}}, {{lc|NAN}}. Note that C++ takes no special notice of signalling NaNs other than detecting their support by {{lc|std::numeric_limits::has_signaling_NaN}}, and treats all NaNs as quiet. | * ''not-a-number'' (NaN), which does not compare equal with anything (including itself). Multiple bit patterns represent NaNs, see {{lc|std::nan}}, {{lc|NAN}}. Note that C++ takes no special notice of signalling NaNs other than detecting their support by {{lc|std::numeric_limits::has_signaling_NaN}}, and treats all NaNs as quiet. | ||
Line 225: | Line 221: | ||
{{rlp|implicit conversion|Implicit conversions}} are defined between floating types and integer types. | {{rlp|implicit conversion|Implicit conversions}} are defined between floating types and integer types. | ||
− | See | + | See {{lsd|cpp/types/climits#Limits of floating-point types}} and {{lc|std::numeric_limits}} for additional details, limits, and properties of the floating-point types. |
===Range of values=== | ===Range of values=== | ||
Line 246: | Line 242: | ||
Note: actual (as opposed to guaranteed minimal) limits on the values representable by these types are available in [[cpp/types/climits|C numeric limits interface]] and {{lc|std::numeric_limits}}. | Note: actual (as opposed to guaranteed minimal) limits on the values representable by these types are available in [[cpp/types/climits|C numeric limits interface]] and {{lc|std::numeric_limits}}. | ||
+ | |||
+ | ===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''' ({{c/core|int}} is 16-bit, {{c/core|long}} and pointer are 32-bit) | ||
+ | ::* Win16 API | ||
+ | :*'''ILP32''' or '''4/4/4''' ({{c/core|int}}, {{c/core|long}}, and pointer are 32-bit); | ||
+ | ::* Win32 API | ||
+ | ::* Unix and Unix-like systems (Linux, macOS) | ||
+ | |||
+ | 64 bit systems: | ||
+ | :* '''LLP64''' or '''4/4/8''' ({{c/core|int}} and {{c/core|long}} are 32-bit, pointer is 64-bit) | ||
+ | ::* [https://learn.microsoft.com/en-us/windows/win32/desktop-programming Win32 API] (also called the Windows API) with compilation target {{enwiki|AArch64|64-bit ARM}} (AArch64) or {{enwiki|x86-64}} (a.k.a. x64) | ||
+ | :* '''LP64''' or '''4/8/8''' ({{c/core|int}} is 32-bit, {{c/core|long}} and pointer are 64-bit) | ||
+ | ::* Unix and Unix-like systems (Linux, macOS) | ||
+ | |||
+ | Other models are very rare. For example, '''ILP64''' ('''8/8/8''': {{c/core|int}}, {{c/core|long}}, and pointer are 64-bit) only appeared in some early 64-bit Unix systems (e.g. {{enwiki|UNICOS|UNICOS on Cray}}). | ||
===Notes=== | ===Notes=== | ||
Line 255: | Line 269: | ||
===Keywords=== | ===Keywords=== | ||
− | {{ltt|cpp/keyword/void}}, | + | {{ltt|cpp/keyword/void}}, |
− | {{ltt|cpp/keyword/bool}}, | + | {{ltt|cpp/keyword/bool}}, |
− | {{ltt|cpp/keyword/true}}, | + | {{ltt|cpp/keyword/true}}, |
− | {{ltt|cpp/keyword/false}}, | + | {{ltt|cpp/keyword/false}}, |
− | {{ltt|cpp/keyword/char}}, | + | {{ltt|cpp/keyword/char}}, |
− | {{ltt|cpp/keyword/ | + | {{ltt|cpp/keyword/char8_t}}, |
− | + | {{ltt|cpp/keyword/char16_t}}, | |
− | + | {{ltt|cpp/keyword/char32_t}}, | |
+ | {{ltt|cpp/keyword/wchar_t}}, | ||
{{ltt|cpp/keyword/int}}, | {{ltt|cpp/keyword/int}}, | ||
{{ltt|cpp/keyword/short}}, | {{ltt|cpp/keyword/short}}, | ||
Line 270: | Line 285: | ||
{{ltt|cpp/keyword/float}}, | {{ltt|cpp/keyword/float}}, | ||
{{ltt|cpp/keyword/double}} | {{ltt|cpp/keyword/double}} | ||
− | |||
− | |||
− | |||
− | |||
− | |||
===Defect reports=== | ===Defect reports=== | ||
{{dr list begin}} | {{dr list begin}} | ||
{{dr list item|wg=cwg|dr=238|std=C++98|before=the constraints placed on a floating-point implementation was unspecified|after=specified as<br>no constraint}} | {{dr list item|wg=cwg|dr=238|std=C++98|before=the constraints placed on a floating-point implementation was unspecified|after=specified as<br>no constraint}} | ||
− | {{dr list item|wg=cwg|dr=1759|std= | + | {{dr list item|wg=cwg|dr=1759|std=C++11|before={{c/core|char}} is not guaranteed to be able to represent UTF-8 code unit 0x80|after=guaranteed}} |
+ | {{dr list item|wg=cwg|dr=2689|std=C++11|before=cv-qualified {{lc|std::nullptr_t}} was not a fundemental type|after=it is}} | ||
{{dr list item|wg=cwg|dr=2723|std=C++98|before=the ranges of representable values for floating-point types were not specified|after=specified}} | {{dr list item|wg=cwg|dr=2723|std=C++98|before=the ranges of representable values for floating-point types were not specified|after=specified}} | ||
{{dr list item|paper=P2460R2|std=C++98|before={{c/core|wchar_t}} was required to be able to represent distinct codes for all members<br>of the largest extended character set specified among the supported locales|after=not required}} | {{dr list item|paper=P2460R2|std=C++98|before={{c/core|wchar_t}} was required to be able to represent distinct codes for all members<br>of the largest extended character set specified among the supported locales|after=not required}} | ||
{{dr list end}} | {{dr list end}} | ||
+ | |||
+ | ===References=== | ||
+ | {{ref std c++23}} | ||
+ | {{ref std|section=6.8.2|title=Fundamental types|id=basic.fundamental}} | ||
+ | {{ref std end}} | ||
+ | {{ref std c++20}} | ||
+ | {{ref std|section=6.8.1|title=Fundamental types|id=basic.fundamental}} | ||
+ | {{ref std end}} | ||
+ | {{ref std c++17}} | ||
+ | {{ref std|section=6.9.1|title=Fundamental types|id=basic.fundamental}} | ||
+ | {{ref std end}} | ||
+ | {{ref std c++14}} | ||
+ | {{ref std|section=3.9.1|title=Fundamental types|id=basic.fundamental}} | ||
+ | {{ref std end}} | ||
+ | {{ref std c++11}} | ||
+ | {{ref std|section=3.9.1|title=Fundamental types|id=basic.fundamental}} | ||
+ | {{ref std end}} | ||
+ | {{ref std c++03}} | ||
+ | {{ref std|section=3.9.1|title=Fundamental types|id=basic.fundamental}} | ||
+ | {{ref std end}} | ||
+ | {{ref std c++98}} | ||
+ | {{ref std|section=3.9.1|title=Fundamental types|id=basic.fundamental}} | ||
+ | {{ref std end}} | ||
===See also=== | ===See also=== | ||
− | * {{rlp|type| | + | * {{rlp|type|The C++ type system overview}} |
− | * {{rlp|cv| | + | * {{rlp|cv|Const-volatility (cv) specifiers and qualifiers}} |
− | * {{rlp|storage duration| | + | * {{rlp|storage duration|Storage duration specifiers}} |
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc see c|c/language/arithmetic types|arithmetic types| nomono=true}} | + | {{dsc see c|c/language/arithmetic types|arithmetic types|nomono=true}} |
{{dsc end}} | {{dsc end}} | ||
{{langlinks|ar|de|es|fr|it|ja|pl|pt|ru|zh}} | {{langlinks|ar|de|es|fr|it|ja|pl|pt|ru|zh}} |
Latest revision as of 22:33, 13 August 2024
(See also type for type system overview and the list of type-related utilities that are provided by the C++ library)
The following types are collectively called fundamental types :
- (possibly cv-qualified) void
|
(since C++11) |
Contents |
[edit] void
- void — type with an empty set of values. It is an incomplete type that cannot be completed (consequently, objects of type void 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.
std::nullptr_t
std::nullptr_t is the type of the null pointer literal, sizeof(std::nullptr_t) is equal to sizeof(void*). |
(since C++11) |
[edit] Integral types
[edit] Standard integer types
- int — basic integer type. The keyword int 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 representation
- Size:
- 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.
|
(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 standard integer types and their properties in various common data models:
Type specifier | Equivalent type | Width in bits by data model | ||||
---|---|---|---|---|---|---|
C++ standard | LP32 | ILP32 | LLP64 | LP64 | ||
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).
Extended integer typesThe extended integer types are implementation-defined. Note that fixed width integer types are typically aliases of the standard integer types. |
(since C++11) |
[edit] Boolean type
- bool — integer type, capable of holding one of the two values:
true
orfalse
. The value of sizeof(bool) is implementation defined and might differ from 1.
[edit] Character types
Character types are integer types used for a character representation.
- 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.
|
(since C++11) |
|
(since C++20) |
Besides the minimal bit counts, the C++ Standard guarantees that
- 1 == sizeof(char) ≤ sizeof(short) ≤ sizeof(int) ≤ sizeof(long) ≤ sizeof(long long).
Note: this allows the extreme case in which bytes are sized 64 bits, all types (including char) are 64 bits wide, and sizeof
returns 1 for every type.
[edit] Floating-point types
[edit] Standard floating-point types
The following three types and their cv-qualified versions are collectively called standard floating-point types.
- float — single precision floating-point type. Usually IEEE-754 binary32 format.
- double — double precision floating-point type. Usually IEEE-754 binary64 format.
- long double — extended precision floating-point type. Does not necessarily map to types mandated by IEEE-754.
- IEEE-754 binary128 format is used by some HP-UX, SPARC, MIPS, ARM64, and z/OS implementations.
- The most well known IEEE-754 binary64-extended format is x87 80-bit extended precision format. It is used by many x86 and x86-64 implementations (a notable exception is MSVC, which implements long double in the same format as double, i.e. binary64).
- On PowerPC double-double can be used.
Extended floating-point typesThe extended floating-point types are implementation-defined. They may include fixed width floating-point types. |
(since C++23) |
[edit] Properties
Floating-point types may support special values:
- infinity (positive and negative), see INFINITY
- the negative zero, -0.0. It compares equal to the positive zero, but is meaningful in some arithmetic operations, e.g. 1.0 / 0.0 == INFINITY, but 1.0 / -0.0 == -INFINITY), and for some mathematical functions, e.g. sqrt(std::complex)
- not-a-number (NaN), which does not compare equal with anything (including itself). Multiple bit patterns represent NaNs, see std::nan, NAN. Note that C++ takes no special notice of signalling NaNs other than detecting their support by std::numeric_limits::has_signaling_NaN, and treats all NaNs as quiet.
Floating-point numbers may be used with arithmetic operators +, -, /, and * as well as various mathematical functions from <cmath>. Both built-in operators and library functions may raise floating-point exceptions and set errno as described in math errhandling.
Floating-point expressions may have greater range and precision than indicated by their types, see FLT_EVAL_METHOD. Floating-point expressions may also be contracted, that is, calculated as if all intermediate values have infinite range and precision, see #pragma STDC FP_CONTRACT. Standard C++ does not restrict the accuracy of floating-point operations.
Some operations on floating-point numbers are affected by and modify the state of the floating-point environment (most notably, the rounding direction).
Implicit conversions are defined between floating types and integer types.
See limits of floating-point types and std::numeric_limits for additional details, limits, and properties of the floating-point types.
[edit] Range of values
The following table provides a reference for the limits of common numeric representations.
Prior to C++20, the C++ Standard allowed any signed integer representation, and the minimum guaranteed range of N-bit signed integers was from -(2N-1-1) to +2N-1-1 (e.g. −127 to 127 for a signed 8-bit type), which corresponds to the limits of ones' complement or sign-and-magnitude.
However, all C++ compilers use two's complement representation, and as of C++20, it is the only representation allowed by the standard, with the guaranteed range from -2N-1 to +2N-1-1 (e.g. −128 to 127 for a signed 8-bit type).
8-bit ones' complement and sign-and-magnitude representations for char have been disallowed since C++11 (via the resolution of CWG issue 1759), because a UTF-8 code unit of value 0x80 used in a UTF-8 string literal must be storable in a char type object.
The range for a floating-point type T
is defined as follows:
- The minimum guaranteed range is the most negative finite floating-point number representable in
T
through the most positive finite floating-point number representable inT
. - If negative infinity is representable in
T
, the range ofT
is extended to all negative real numbers. - If positive infinity is representable in
T
, the range ofT
is extended to all positive real numbers.
Since negative and positive infinity are representable in ISO/IEC/IEEE 60559 formats, all real numbers lie within the range of representable values of a floating-point type adhering to ISO/IEC/IEEE 60559.
Type | Size in bits | Format | Value range | |
---|---|---|---|---|
Approximate | Exact | |||
character | 8 | signed | −128 to 127 | |
unsigned | 0 to 255 | |||
16 | UTF-16 | 0 to 65535 | ||
32 | UTF-32 | 0 to 1114111 (0x10ffff) | ||
integer | 16 | signed | ± 3.27 · 104 | −32768 to 32767 |
unsigned | 0 to 6.55 · 104 | 0 to 65535 | ||
32 | signed | ± 2.14 · 109 | −2,147,483,648 to 2,147,483,647 | |
unsigned | 0 to 4.29 · 109 | 0 to 4,294,967,295 | ||
64 | signed | ± 9.22 · 1018 | −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | |
unsigned | 0 to 1.84 · 1019 | 0 to 18,446,744,073,709,551,615 | ||
binary floating- point |
32 | IEEE-754 |
|
|
64 | IEEE-754 |
|
| |
80[note 1] | x86 |
|
| |
128 | IEEE-754 |
|
|
- ↑ The object representation usually occupies 96/128 bits on 32/64-bit platforms respectively.
Note: actual (as opposed to guaranteed minimal) limits on the values representable by these types are available in C numeric limits interface and std::numeric_limits.
[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 and long are 32-bit, pointer is 64-bit)
- Win32 API (also called the Windows API) with compilation target 64-bit ARM (AArch64) or x86-64 (a.k.a. x64)
- 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] Notes
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_unicode_characters |
200704L | (C++11) | New character types (char16_t and char32_t) |
__cpp_char8_t |
201811L | (C++20) | char8_t |
202207L | (C++23) | char8_t compatibility and portability fix (allow initialization of (unsigned) char arrays from UTF-8 string literals)
|
[edit] Keywords
void, bool, true, false, char, char8_t, char16_t, char32_t, wchar_t, int, short, long, signed, unsigned, float, double
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 238 | C++98 | the constraints placed on a floating-point implementation was unspecified | specified as no constraint |
CWG 1759 | C++11 | char is not guaranteed to be able to represent UTF-8 code unit 0x80 | guaranteed |
CWG 2689 | C++11 | cv-qualified std::nullptr_t was not a fundemental type | it is |
CWG 2723 | C++98 | the ranges of representable values for floating-point types were not specified | specified |
P2460R2 | C++98 | wchar_t was required to be able to represent distinct codes for all members of the largest extended character set specified among the supported locales |
not required |
[edit] References
- C++23 standard (ISO/IEC 14882:2024):
- 6.8.2 Fundamental types [basic.fundamental]
- C++20 standard (ISO/IEC 14882:2020):
- 6.8.1 Fundamental types [basic.fundamental]
- C++17 standard (ISO/IEC 14882:2017):
- 6.9.1 Fundamental types [basic.fundamental]
- C++14 standard (ISO/IEC 14882:2014):
- 3.9.1 Fundamental types [basic.fundamental]
- C++11 standard (ISO/IEC 14882:2011):
- 3.9.1 Fundamental types [basic.fundamental]
- C++03 standard (ISO/IEC 14882:2003):
- 3.9.1 Fundamental types [basic.fundamental]
- C++98 standard (ISO/IEC 14882:1998):
- 3.9.1 Fundamental types [basic.fundamental]
[edit] See also
- The C++ type system overview
- Const-volatility (cv) specifiers and qualifiers
- Storage duration specifiers
C documentation for arithmetic types
|