Fundamental types
Fundamental types
- void type
- boolean type
- character types
- integer types
- Properties
- Data Models
- Floating-point types
- Floating-point properties
即int为integer type的基本类型,通过添加修饰符(modifiers)signed
or unsigned
和short
or long
or long long
来实现存储空间位数的大小,C++保证每一个类型的最小位数
Type Specifier | Equivalent Type | C++Standard | LP32 | ILP32 | LLP64 | LP64 |
---|---|---|---|---|---|---|
short |
short int |
at least 16 | 16 | 16 | 16 |
16 |
short int |
short int |
at least 16 | 16 | 16 | 16 |
16 |
signed int |
short int |
at least 16 | 16 | 16 | 16 |
16 |
signed short int |
short int |
at least 16 | 16 | 16 | 16 |
16 |
unsigned short |
unsigned short int |
at least 16 | 16 | 16 | 16 |
16 |
unsigned short int |
unsigned short int |
at least 16 | 16 | 16 | 16 |
16 |
int |
int |
at least 16 | 16 | 32 | 32 |
32 |
signed |
int |
at least 16 | 16 | 32 | 32 |
32 |
signed int |
int |
at least 16 | 16 | 32 | 32 |
32 |
unsigned |
unsigned int |
at least 16 | 16 | 32 | 32 |
32 |
unsigned int |
unsigned int |
at least 16 | 16 | 32 | 32 |
32 |
long |
long int |
at least 32 | 32 | 32 | 32 |
64 |
signed long |
long int |
at least 32 | 32 | 32 | 32 |
64 |
signed long int |
long int |
at least 32 | 32 | 32 | 32 |
64 |
unsigned long |
unsigned long int |
at least 32 | 32 | 32 | 32 |
64 |
unsigned long int |
unsigned long int |
at least 32 | 32 | 32 | 32 |
64 |
long long |
long long int |
at least 32 | 64 | 64 | 64 |
64 |
long long int |
long long int |
at least 32 | 64 | 64 | 64 |
64 |
signed long long |
long long int |
at least 32 | 64 | 64 | 64 |
64 |
signed long long int |
long long int |
at least 32 | 64 | 64 | 64 |
64 |
unsigned long long |
long long int |
at least 32 | 64 | 64 | 64 |
64 |
unsigned long long |
unsigned long long int since C++11 |
at least 32 | 64 | 64 | 64 |
64 |
unsigned long long int |
unsigned long long int since C++11 |
at least 32 | 64 | 64 | 64 |
64 |
Besides the minimal bit counts,the C++ Standard guaranteed that
1 == sizeof(char
) <= sizeof(short
) <= sizeof(int
) <= sizeof(long
) <= sizeof(long long
)
Note:integer arithmetic is defined differently for signed and unsigned integer types.See
arithmetic operators
,in particularinteger overflows
Win64 is a LLP64 platform, while Solaris and Linux are LP64 platforms. Thus the only safe way to store pointers in integer types is either always use uintptr_t
(defined in stdint.h not included at least with MSVC2003 and earlier), or always use long long fields.
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)
- Win16API
64 bit systems:
- Win16API
- 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
andpointer
are 64-bit)- Unix and Unix-like systems (Linux, Mac OS X)
- Unix and Unix-like systems (Linux, Mac OS X)
Floating-point types
float
- single precision floating point type.Usually IEEE-754 32 bit floating point type
double
- double precision floating point type. Usually IEEE-754 64 bit floating point type
long double
- extended precision floating point type. Does not necessarily map to types mandated by IEEE-754. Usually 80-bit x87 floating point type on x86 and x86-64 architectures
Floating-point properties
Floating-point types may support special values:
- infinity (positive and negative)
- 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
, but1.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).
Real floating-point numbers may be used with arithmetic operators + - / * and 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.
浮点数的精度表示FLT_EVAL_METHOD和精收缩问题#pragma STDC FP_CONTRACT
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.
Implicit conversions are defined between real 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.
Note: actual (as opposed to guaranteed minimal) limits on the values representable by these types are available in <climits>, <cfloat> and std::numeric_limits