Tips for C ++ Primer Charpter 1, primercharpter

Source: Internet
Author: User

Tips for C ++ Primer Charpter 1, primercharpter
Chapter 2 variables and Basic Types

Basic built-in types: arithmetic type and null type; arithmetic type, including INTEGER (character and boolean type are also included) and floating point type.

 

The minimum addressable memory block is called byte, and the basic unit of storage is called word. A word is usually composed of several bytes.

1 byte = 8 bits (bit). If the bit is not 0, it is 1.

 

Unsigned int can be abbreviated as unsigned.

 

Int is the same as singed int (except for Boolean and extended word types (such as the wide character wchat_t), other integer types (such as short and long) are also similar), except for string type, character types include char, unsigned char, and signed char, but char is different from signed char. There are only two types of characters: signed and unsigned, char is actually one of the above two, which is determined by the compiler.

 

Do not use char or bool in arithmetic expressions. Because char may be signed on some machines, but it may be unsigned on other machines, and an error may occur when char is used. If a small integer is used, specify it as unsigned char or signed char. The bool value is not 0 or 1. It is not suitable for arithmetic expressions.

 

When a value of the unsigned type is assigned to a value that exceeds the value range, the result is the remainder of the modulus of the total number of the value of the unsigned type for the initial value.

For example, an unsigned char occupies 8 bits (1 byte) and can represent a total of 0,255 values of [256]. When a value outside the range is assigned, it is assumed that it is-1, the actual result is (-1) % 256 = 255.

 

When a value of the signed type is assigned to a value that exceeds the indicated range, the result is undefined. In this case, the program may continue to work, crash, or generate junk data.

 

When an arithmetic expression contains both a symbolic number and an int value, the int value is converted to an unsigned number. The process of converting an int to an unsigned number is equivalent to assigning an int to the unsigned type.

Do not mix the signed and unsigned types. If the expression has both the signed type and the signed type, an exception occurs when the value of the signed type is negative, because the number of signed characters is automatically converted to the unsigned number.

For example, if a =-1, B = 1, c = a * B, if both a and B are int, c is-1; if a is int, if B is an unsigned int, the result of c is (-1) % (2 ^ 32) * 1 = 4294967295 (depending on the number of BITs occupied by the int on the current machine, suppose int Is 32bit ).

 

The nominal value of an integer can be written in decimal, octal, or hexadecimal format.

Start with 0: octal

Starting with 0x and 0X: hexadecimal

 

The floating point numeric value can be omitted from the integer or decimal part.

Example: 0 ..,. 001

The floating point numeric value is a double by default.

 

You can specify the type of the nominal value by prefix or suffix.

Prefix

U: char16_t (Unicode16 characters)

U: char32_t (Unicode32 characters)

L: wchar_t (wide character)

U8: char (UTF-8, used only for string literal constants)

Suffix

Nominal value of an integer:

U, U: unsigned

L, L: long

Ll, LL: long

Floating Point-type nominal value:

F, F: float

L, L: long double

 

Initialization and assignment are different concepts.

Initialization: assign an initial value to a variable when it is created.

Assign value: erase the current value of the object and replace it with a new value.

Therefore, the overhead of value assignment is greater than initialization.

 

If a variable of the built-in type is not explicitly initialized, its value is determined by the defined position.

Defined in any function External: initialized to 0

Defined in the function body: will not be initialized, and its value is undefined

Note: The pointer type is not a basic built-in type, but it also has the above features.

 

If the class object does not display initialization, its value is determined by the class. (Default constructor of the Class)

 

The Declaration defines the object associated with the name to make the name known to the program.

If you want to declare a variable rather than define it, add the keyword extern before the variable name, instead of explicitly initializing the variable.

For example:

Extern int I; // declare I instead of defining I

Int j; // declare and define j

 

Explicit initialization declaration is defined.

Extern double pi = 3.14; // definition; this is allowed in the external body of the function, but the existence of the extern keyword makes no sense.

A variable marked by the extern keyword cannot be initialized within the function body.

 

The reference itself is not an object, and there is no actual address (if you try to get an "referenced address" with the address operator, what you actually get is the address of the referenced object ), it is only the alias of an existing object. You cannot define a reference or a pointer to the reference.

The reference must be initialized because it cannot re-bind the reference to another object.

All referenced types must match the bound objects strictly (two exceptions will be mentioned later ).

 

The reference must be bound to an object and cannot be bound to the nominal value or the calculation result of an expression.

 

A pointer is an object that allows assignment and copying of pointers. A pointer can point to different objects within its lifecycle.

You do not need to assign an initial value when defining a pointer. Similar to the basic built-in type, if the pointer defined in all functions in vitro is not initialized, the default initial value 0 will exist, and the pointer defined in the block scope will have an uncertain value.

The types of all pointers must match those of the specified object strictly (except for two cases, which will be mentioned later ).

 

&, * Appears in the declaration statement to form a composite type; & appears in the expression, which is an address operator; * appears in the expression, is a quote (or multiplication operator ).

 

Nullptr = NULL = 0

 

Void * is a special pointer type that can be used to store the address of any object. However, you cannot directly operate on the objects referred to by the void * pointer, because the object type determines the operations that can be performed on the object, and we do not know the object type.

 

Pointer to pointer

A pointer is an object in memory and has its own address like other objects. Therefore, you can store the pointer address in another pointer.

The number of X is used to identify the pointer level. ** Is a pointer to a pointer, ** is a pointer to a pointer, and so on.

Int I = 1024;

Int * pi = & I; // The number of pi points to an int type.

Int * ppi = & pi; // ppi points to an int type pointer.

When an int pointer is referenced, an int number is obtained. When a pointer pointing to a pointer is referenced, a pointer is obtained.

There are three methods to obtain the I value: I, * pi, and ** ppi.

 

Related Article

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.