2.1 基本类型 | Fundamental Types

基本内置类型 fundamental types

Reference

基本内置类型分为:

  • 算术类型(arithmetic type)
  • 空类型(void)
  • 空指针(nullptr) std::nullptr_t (since C++11)

数据模型Data models

Type \ Model LP32 IPL32 LP64 ILP64 LLP64
char 8 8 8 8 8
short 16 16 16 16 16
int 16 32 32 64 32
long 32 32 64 64 32
long long 64 64 64 64 64
pointer 32 32 64 64 64


算术类型arithmetic type

判断方式:std::is_arithmetic

Boolean type-布尔类型

true or false, sizeof(bool) is implementation defined and might differ from 1.

Integer type-整型

Screenshot 2021-03-01 154806.png

最小长度的规则:1 == sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)

floating type-浮点型

  • float - single precision floating point type. Matches IEEE-754 32 bit floating point type if supported.

  • double - double precision floating point type. Matches IEEE-754 64 bit floating point type if supported

  • long double - extended precision floating point type. Matches IEEE-754 extended floating-point type if supported, otherwise matches some non-standard extended floating-point type as long as its precision is better than double and range is at least as good as double, otherwise matches the type double. Some x86 and x86_64 implementations use the 80-bit x87 floating point type.

  • Special Values

    • infinity
    • negative zero
    • not-a-number(NaN)
  • Complex floating type-复浮点型

    • float _Complex (also available as float complex if is included)
    • double _Complex (also available as double complex if is included)
    • long double _Complex (also available as long double complex if is included)
    • any order is permitted: long double complex, complex long double, and even double complex long name the same type.
  • Imaginary floating type-虚浮点型

character type-字符类型

  • 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). (具体表现为signed还是unsigned由编译器决定,因此不要使用此类型储值,加上signed or unsigned). Multibyte characters strings use this type to represent code units. The character types are large enough to represent any UTF-8 eight-bit code unit (since C++14). 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). Required to be large enough to represent any supported character code point (32 bits on systems that support Unicode. A notable exception is Windows, where wchar_t is 16 bits and holds UTF-16 code units) It has the same size, signedness, and alignment as one of the integer types, but is a distinct type.
  • char16_t (since C++11) - 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 (since C++11) - 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.
  • char8_t (since C++20) - 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.

Range of Values-数值范围

Screenshot 2021-03-01 155107.png

类型转换

  • Non-bool -> bool: 非零则True
  • bool -> Non-bool: True为1,False为0
  • float -> int: 保留小数点前
  • int -> float: 小数记为0,若超限则损失精度
  • 某个值 -> unsigned超范围: 对无符号类型可表示的总数取模后的余数
  • 某个值 -> signed超范围: undefined,可能工作,可能崩溃,可能生成垃圾数据

无符号数混合表达式

  • int + unsigned: 强行转换int为unsigned,若int为负则取模的余数

    #include 
    unsigned a = -10;
    int b = -10;
    int main() {
        std::cout << a + b << std::endl;
        return 0;
    }
    

    unsigned int最大值为4294967295,该结果为4294967276

  • unsigned - unsigned: 保证结果不为负,否则取模

    unsigned a = 10, b = 20;
    int main() {
        std::cout << a - b << std::endl;
        std::cout << b - a << std::endl;
        return 0;
    }
    

    同理,结果为4294967286 (unsigned -10)


字面值常量-Literal

Literals are the tokens of a C++ program that represent constant values embedded in the source code.
参见

  • integer literals are decimal, octal, hexadecimal or binary numbers of integer type.
  • character literals are individual characters of type
    • char or wchar_t
    • char16_t or char32_t (since C++11)
    • char8_t (since C++20)
  • floating-point literals are values of type float, double, or long double
  • string literals are sequences of characters of type
    • const char[] or const wchar_t[]
    • const char16_t[] or const char32_t[] (since C++11)
    • const char8_t[] (since C++20)
  • boolean literals are values of type bool, that is true and false
  • nullptr is the pointer literal which specifies a null pointer value (since C++11)
  • user-defined literals are constant values of user-specified type (since C++11)

注意: short无字面值,string不是字面值类型

整型字面值-Integer Literal

ref

Syntax
(1) decimal-literal integer-suffix(optional)
(2) octal-literal integer-suffix(optional)
(3) hex-literal integer-suffix(optional)
(4) binary-literal integer-suffix(optional) (since C++14)


十进制-decimal-literal

八进制-octal-literal: 0开头的数字,如00,01,02

十六进制-hex-literal: 0X, 0x开头的数字/字母,如0X0,0X1,0x2

二进制-binary-literal: 0B, 0b开头的0/1

整型后缀-integer-suffix:

  • unsigned-suffix: u, U
  • long-suffix: l, L, long-long-suffix(since C++11):ll, LL

插入引号:上述字面值均可通过插入引号(')来分割数字(since C++14)。以下四个变量实际字面值相同:

unsigned long long l1 = 18446744073709550592ull; // C++11
unsigned long long l2 = 18'446'744'073'709'550'592llu; // C++14
unsigned long long l3 = 1844'6744'0737'0955'0592uLL; // C++14
unsigned long long l4 = 184467'440737'0'95505'92LLU; // C++14

浮点字面值-Floating point Literal

Syntax
(1) digit-sequence exponent suffix(optional)
(2) digit-sequence . exponent(optional) suffix(optional)
(3) digit-sequence(optional) . digit-sequence exponent(optional) suffix(optional)
(4) 0x/0X hex-digit-sequence exponent suffix(optional) (since C++17)
(5) 0x/0X hex-digit-sequence . exponent suffix(optional) (since C++17)
(6) 0x/0X hex-digit-sequence(optional) . hex-digit-sequence exponent suffix(optional) (since C++17)

示例:

(1) 1e10, 1e-5L

(2) 1., 1.e-5

(3) .5f, 2.e-8l

注意,十六进制科学计数法中e会有冲突,换为p/P (sinceC++17):

(4) 0x1fp10, 0X1fP10

(5) 0x1.pf, 0Xe.p-1

(6) 0x0.1p-1, 0Xe.epeL

后缀suffix:

  • (no suffix) defines double
  • f F defines float
  • l L defines long double

字符字面值-Character Literal

Syntax
(1) 'c-char'
(2) u8'c-char' (since C++17)
(3) u'c-char' (since C++11)
(4) U'c-char' (since C++11)
(5) L'c-char'
(6) 'c-char-sequence'


c-char is either:

  • a character from the source character set minus single-quote ('), backslash (\), or the newline character,
  • escape sequence 转义字符
  • universal character name 通用字符名

c-char-sequence is a sequence of two or more c-chars.

  • narrow character literal or ordinary character literal, e.g. 'a' or '\n' or '\13'. Such literal has type char and the value equal to the representation of c-char in the execution character set.
  • UTF-8 character literal, e.g. u8'a'. Such literal has type char (until C++20)char8_t (since C++20) and the value equal to ISO 10646 code point value of c-char,
  • UTF-16 character literal, e.g. u'貓', but not u'' (u'\U0001f34c'). Such literal has type char16_t and the value equal to ISO 10646 code point value of c-char
  • UTF-32 character literal, e.g. U'貓' or U''. Such literal has type char32_t and the value equal to ISO 10646 code point value of c-char.
  • wide character literal, e.g. L'β' or L'貓'. Such literal has type wchar_t and the value equal to the value of c-char in the execution wide character set.
  • Multicharacter literal, e.g. 'AB', has type int and implementation-defined value.

字符串字面值-String Literal

Syntax explanation
(1) " (unescaped_character/escaped_character) " Narrow multibyte string literal. The type of an unprefixed string literal is const char[N]
(2) L" (unescaped_character/escaped_character) " Wide string literal. The type of a L"..." string literal is const wchar_t[N]
(3) u8" (unescaped_character/escaped_character) " (since C++11) UTF-8 encoded string literal. The type of a u8"..." string literal is const char[N] (until C++20)const char8_t[N] (since C++20)
(4) u" (unescaped_character/escaped_character) " (since C++11) UTF-16 encoded string literal. The type of a u"..." string literal is const char16_t[N]
(5) U" (unescaped_character/escaped_character) " (since C++11) UTF-32 encoded string literal. The type of a U"..." string literal is const char32_t[N]
(6) prefix(optional) R"delimiter(raw_characters)delimiter" (since C++11) Raw string literal. Used to avoid escaping of any character. Anything between the delimiters becomes part of the string.

布尔字面值-Boolean Literal

Syntax
(1) true
(2) false

nullptr, the pointer literal

The keyword nullptr denotes the pointer literal. It is a prvalue of type std::nullptr_t. There exist implicit conversions from nullptr to null pointer value of any pointer type and any pointer to member type. Similar conversions exist for any null pointer constant, which includes values of type std::nullptr_t as well as the macro NULL.

User-defined literals(since C++11)

https://en.cppreference.com/w/cpp/language/user_literal

escape sequences & universal character name 转义字符&通用字符名:

两类不可直接使用的字符:不可打印(nonprintable)、特殊字符

Escape sequence Description Representation
\' single quote byte 0x27 in ASCII encoding
\" double quote byte 0x22 in ASCII encoding
\? question mark byte 0x3f in ASCII encoding
\\ backslash byte 0x5c in ASCII encoding
\a audible bell byte 0x07 in ASCII encoding
\b backspace byte 0x08 in ASCII encoding
\f form feed - new page byte 0x0c in ASCII encoding
\n line feed - new line byte 0x0a in ASCII encoding
\r carriage return byte 0x0d in ASCII encoding
\t horizontal tab byte 0x09 in ASCII encoding
\v vertical tab byte 0x0b in ASCII encoding
\nnn arbitrary octal value, e.g.:\12==\n: byte nnn
\xnn arbitrary hexadecimal value byte nn
\unnnn (since C++11) universal character name(arbitrary Unicode value); may result in several characters code point U+nnnn
\Unnnnnnnn (since C++11) universal character name(arbitrary Unicode value); may result in several characters code point U+nnnnnnnn

Unicode identifiers reference: https://en.cppreference.com/w/cpp/language/identifiers

Type Alias 类型别名

https://en.cppreference.com/w/cpp/language/type_alias

你可能感兴趣的:(2.1 基本类型 | Fundamental Types)