Primer C++ Fifth Edition Chapter 2 Basic Built-in Types

1. Arithmetic types

[Differentiation]
1 Byte (byte) = 8 bit (number of bits)

Word is composed of 4 or 8 bytes. The
arithmetic type is divided into integer type and empty type

1.1 Integer

1.1.1 Boolean type

[Bool]
There is no minimum size

The value can only be true (true) or false (false)

1.1.2 Character type

char character

Extended character set:

wchar_t wide character
Unicode character:

char16_t
char32_t

1.1.3 Integer

short
int
long
long long

1.2 Floating point

Single-precision float (1 word) 7-digit
double-precision double (2 words) 16-digit
extended precision value long double (3 or 4 words) According to requirements

1.3 Signed and unsigned types

The signed type includes positive numbers, negative numbers, and 0.
Unsigned types include positive numbers and 0.

1.3.1 "Special" character type

The character type is divided into the following three types:
char
signed char
unsigned char

Char is different from unsigned char. Because characters are divided into three categories, but there are only two symbol types , so: The
compiler determines whether the char type is signed or not.

1.3.2 Unsigned char different from "theory"

In theory, it is -127 to 127, and in
practical application, it is -128 to 127

1.4 Type selection

① If the value exceeds the range of the int type, select the long long type.

② Try to avoid bool and char in arithmetic expressions.

Because the char type is unsigned on some machines and signed on some machines, using unsigned char or signed char is helpful for distinguishing signs.

③ When it is a floating point type, use the double type .

1.5 Type conversion

1.5.1 Boolean and non-Boolean types

Non-Boolean conversion to Boolean:
As long as the value is not 0, it is all true, if the value is 0, it is false.

Boolean conversion to non-Boolean: the
initial value is false, which is 0; the initial value is true, which is 1.

2 Literal constants-"see at a glance"

Each literal constant corresponds to a data type

The form and value of a character constant determine its data structure

2.1 Integer and floating point literals

Starting with 0 is octal (020)
starting with 0x or 0X is hexadecimal (0x67)

Types of decimal literals : int, long, long long, the smallest size (int)

short has no literal constant

Strictly speaking: decimal literal constants do not have negative numbers-
42 is a negative literal constant, but the negative sign is not in the literal constant.
Its function is only to take a negative value for the character value.

The floating-point literal value is a double , the exponent expressed in scientific notation, and the exponent part is expressed by e or E:
1.23e3 == 1230

2.2 Characters and string literals

Character literal value: 'a'
String literal value: "a"
The actual length of the string literal value is one more than its content
For example:"A" = 'a' + '\0' (empty character)

Multi-line string literal:

std::cout << "a really things "
             "that is ...." << std::endl;

2.3 Escape sequence

Two types that cannot be used directly:
1. Non-printable characters
No visible icons, such as backspace or other control characters
2. Characters with special meaning
Single quote, double quote, question mark, backslash

Escape sequence: withBackslashStart
\ \ (backslash)
\? (Question mark)
\n (line feed)
\ "(double quotation mark)
\ '(single quotation mark)

\ x followed by one or more hexadecimal digits
\ followed by one, two or three octal digits (More than 3 numbers, only 3 valid

example:

\ 115 (character M)
\ x4d (character M)
\ 7 (bell)
\ 12 (newline character)
\ 0 (empty character)

 std::cout << "hi, \x4d O \115 ! " << std::endl;
 //输出:hi, M O M !

2.4 Boolean literals and pointer literals

Boolean literal values: true and false

Pointer literal: nullptr

Guess you like

Origin blog.csdn.net/weixin_42198265/article/details/112706578