C + + Primer 5 notes 2 Chapter 2 variables and basic types (2.1~2.3)

Source: Internet
Author: User
Tags arithmetic

The second chapter is variable and basic type 2.1 basic built-in type 2.1.1 arithmetic type

A, C + + c more than a few arithmetic types wchar_t, char16_t, char32_t
Where wchar_t is a wide character, char16_t, char32_t is a Unicode character, the question comes, what is the difference between wide characters and Unicode?
Open Baidu a bit, you don't know the wide character, we can know
the characters represented by multiple bytes are called wide characters, while Unicode is only an implementation of wide character encodings, and the wide characters are not necessarily Unicode.

B. A new type is defined in C11 a long long takes 8 bytes

C, floating-point type can represent single-precision, double-precision, extended double precision, general float, double type has 7, 16 significant bits.
You can also understand why the float type compares with 0 with fabs (x) < 1e-6.

D. General requirements to know that data cannot be negative when you can opt for unsigned type unsigned

E. Arithmetic operations try not to use char and bool, on different machines char requires explicit unsigned char and signed Char

F. Use double when performing floating-point arithmetic, because float is usually not accurate enough and the cost of the calculation is not very good

2.1.2 Type Conversions

A. When assigning a value to a data type that exceeds the range of his data, the result is undefined

B. When the unsigned and signed types are present in an arithmetic expression, signed is converted to unsigned, which requires special attention

C. Unsigned number will not be less than 0, write to the for loop, prone to the case of a dead loop <-_->!!
This oneself seems to have actually written, in MFC use UINT type of time ...

D.uint_max = 4294967295
singed int is interpreted as unsigned int type, this can be done:
uNsIg Ned _INT=2 32 + 1 +sIg Ned _INT,WheNsIg Ned _INT<0

2.1.3 Literal constants

A. By default, a literal constant is the smallest data type that can hold the data
For negative numbers, symbols are not counted in literal values
Floating-point literal literal is double

B. If the two string literal positions are contiguous and have only spaces, indents, and newline characters separated, it is actually still a whole.

std::cout<<"a really, really long string literal"        "that spans two lines"<<std::endl;

C. Generalized escape sequences, \x table 16 binary data, \ data Table 8 binary (up to three bits)

D. \a, \7 Bell

2.2 Variable 2.2.1 Variable definition

A. for C + +, "variables" and "objects" are generally used interchangeably

B. Object: A block capable of storing data and having some kind of memory space

C. In the same definition statement, you can initialize the following variables with the values of the variables defined first

D. Relationship of initialization and assignment
Initialization is not an assignment, the initialization is to create a variable and assign it an initial value, the assignment is to erase the current value of the object and replace it with a new value.

E. When the curly braces are initialized, if the initial value is at risk of loss, the compiler will make an error and the other way will be implicitly converted

F. The built-in type outside of the function body is initialized to 0, and the function body does not
And the class object has its own decision

2.2.2 The relationship between a variable declaration and a definition

A. In the body of the function, an attempt to initialize a variable with an extern tag will cause an error

When placed outside the function body, it's just a warning.

B.

    externint// 声明    int i;        // 定义    externint4// 定义

C. C + + int J; Direct definition

This code is compiled with GCC, which can be used to illustrate the global variable int i in C; it's just a declaration, not a definition. The same code cannot be passed in g++ because it has multiple definitions and needs to show the use of extern

d. The variable definition in C must be at the beginning of the zone, C + + does not require

E. C + + can access the overridden parent object through the domain operator

2.3 Composite Object 2.3.1 Reference

A. Reference is an alias
Reference is not an object, he is just another name for an already existing object

B. A reference can only be bound to an object and cannot be tied to the result of a literal or an expression

2.3.2 Pointer

A. Unlike references, the pointer itself is an object that allows assignment and copying of pointers, and can point to multiple different objects in the lifetime of a pointer, without assigning a value at the time of definition

B. The reference to the pointer is obtained by referring to the object, so the pointer must be valid at this point

C. nullptr used to initialize pointers, he is a special type of literal value
NULL can also be used to initialize pointers, but he is a preprocessing variable defined in Cstdlib

D. The reference is exclusive, the pointer is a flower-heart

E. assignment forever changes the object to the left of the equals sign

F. void* pointer, only memory address, no access to memory space

2.3.3 Understanding the declaration of a composite type

A. The reference itself is not an object, so a pointer to a reference cannot be defined. But the pointer is an object, so you can have a reference to the pointer

B. read from right to left when you understand a more complex pointer or a referenced declaration statement

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Primer 5 notes 2 Chapter 2 variables and basic types (2.1~2.3)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.