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 <complex.h> is included)
    • double _Complex (also available as double complex if <complex.h> is included)
    • long double _Complex (also available as long double complex if <complex.h> 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 <iostream>
    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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,793评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,567评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,342评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,825评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,814评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,680评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,033评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,687评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,175评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,668评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,775评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,419评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,020评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,978评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,206评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,092评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,510评论 2 343

推荐阅读更多精彩内容