C++ Primer(英语第5版) 阅读日记 - 20200412

接上篇。晚上看书,效率倍加倍。

Chapter 2. Variables and Basic Types

Arithmetic types

  • characters
  • integers
  • boolean values
  • floating-point variables

Void type: void type has no associated values and could be used in only a few circumstances, most commonly as the return type for functions that do not return a value.

Arithmetic types 通常分为两类:intergral types(应该翻译做整型,包括char和boolean)还有 floating-point types (浮点型,很好理解是小时嘛)。

常用类型的大小:

类型 变量大小
char 1 byte
short 2 bytes
int 2 bytes
long 4 bytes
long long 8 bytes
float 4 bytes (typically)
double 8 bytes (typically)

extended character types: char16_t, char32_t 是为Unicode字符设计的,所以更加大。关于Unicode和Utf-8, GBK关系我又忘了,改天梳理一下。这里留个坑,以后链接到这里。

Signed and unsigned types

除了bool和extended character types,intergral types都应该是有符号的:signed或者unsigned。

如果我们强行分配超出范围的值给一个变量,会发生什么?

  • 如果是unsigned的整型,多余的bit会被直接切掉
  • 如果是signed整型的话,结果就会undefined。意味着乱码,崩溃等有可能发生。而且compiler可能不报错。

Literals

A value, such as 42, is known as a literal because its value self-evident. Every literal has a type.

 

你可能感兴趣的:(C++,c++)