Chapter 2 variables and basic types, Chapter 2 variable types

Source: Internet
Author: User

Chapter 2 variables and basic types, Chapter 2 variable types

1. Bit, byte, word, KB

Bit, the smallest data unit in an electronic computer. The status of each digit can only be 0 or 1.

Byte, minimum addressable memory block, basic unit of storage, 1 byte = 8 bit.

Word, the unit of data processing and computation. Different machines have different word lengths. On 32-bit machines, 1 word = 4 bytes = 32-bit; on 64-bit machines, 1 word = 8 bytes = 64-bit.

1KB = 1024B (bytes)

One char data occupies 8 bits and one byte. One English letter occupies 1 byte, and one Chinese Character occupies 2 bytes.

Range. If data type F occupies n digits, the unsigned type is 0 ~ 2n-1, signed type:-2n-1 ~ 2n-1-1.

2. We recommend that you use int to perform integer operations and double to perform floating point operations.

3. If a value of the unsigned type exceeds the value range, the result is the remainder of the modulus of the total number of values of the unsigned type. As shown in the following example:

1 unsigned char c =-1; // The value of c is actually 255
2 unsigned char d = 257; // the actual value of d is 1
Note that mod and rem operations are different. Modulo is the value of negative infinity. The remainder is the value of 0. When the operands are negative, the two are different.

-1 (mod) 256 =-1 + 255;-1 (rem) 256 = 0 + (-1 );

4. Do not abuse the unsigned type. See the following two examples.

1) endless loops.

1 for (unsigned u = 10; u >= 0; --u)2     do sth;

Here, when u = 0 and -- u is executed, because u is of the unsigned type, u = 232-1 instead of-1. In this way, the entire program will be an endless loop.

2) calculate the number of unsigned symbols and the number of signed symbols. The number is automatically converted to the number of unsigned symbols.

1 int I = 10; 2 unsigned u = 42; 3 // The expected output is-32, the actual output is-32 + 2 ^ 32, 42949672644 cout <I-u <endl;

5. the type of octal and hexadecimal literal values is the smallest size among the types that can accommodate the numeric values. The floating point font value is double by default.

6. The two strings are adjacent to each other and only separated by spaces, indentation, and line breaks. The character string written below in multiple rows is actually a literal value.

1 // name of a string written in multiple lines; 2 cout <"a really, really long string literal" 3 "that spans to two lines" <endl;

7. escape characters, starting with a backslash, such as \ n \ t. What is interesting is \ a, which generates a bi alarm. In addition, you can also use generalized escape sequences, followed by no more than three Octal numbers, followed by one hexadecimal number after \ x. \ N = \ 12 = \ xa, all three are line breaks.

8. One statement can define multiple variables and assign initial values to some of them. It can be understood that they are independent of each other, but they only follow the same type specifier.

1 int sum = 0, value, units_sold = 0;

9. In C ++, initialization and assignment are not the same. Initialization is to assign an initial value to a variable when it is created, and the value assignment is to erase the current value of the object and replace it with a new value. You know, during initialization, the variable was just created and there is no current value available for erasure.

10. Variable initialization. The following four statements can be completed.

1 int a = 0;2 int a = {0};3 int a{0};4 int a(0);

Among them, 2nd and 3 methods use curly braces {}. This method is called list initialization. This method has been widely used in the new C ++ 11 standard. One of its important features is that the initial value may cause information loss and the compiler reports an error. As shown below

1 double d = 3.14; 2 int a {d}, B = {d}; // error, loss of precision 3 int c (d), e = d; // correct, type conversion, but precision is also lost

However, you cannot use '()' When initializing data members in a class. You can use either of the other three methods.

11. If the variable is not initialized, the default Initialization is executed.

1) variables defined in the function body will be initialized to 0.

2) variables defined in the function body will not be initialized, and their values are undefined.

3) the class object is not initialized, and its value is determined by the class.

12. Separated compilation mechanism, the program can be divided into multiple files. To share variables between files, separate declarations and definitions. One definition, multiple declarations and usage. Use the extern keyword and do not initialize the display.

1 extern int I; // declaration instead of defining i2 extern int j = 1; // Definition

13. Access the variable with the same name in the global scope within the block scope, which can be implemented using the domain operator.

1 # include <iostream> 2 using namespace std; 3 // This program is only used for description. It is not recommended that a new variable with the same name as a global variable be defined in the function; 4 int a = 42; // global variable, global scope 5 void main () 6 {7 int a = 10; // local variable, block Scope 8 cout <a <'\ n '; // output 10 9 cout <: a <'\ n'; // output 42 10}

14. Reference

1) The reference is bound with the initial value, rather than copying the initial value to the reference.

2) The reference cannot be re-bound. It is always bound with the initial object and must be initialized.

3) A reference is not an object, but an alias of an existing object. A reference cannot be defined. Reference is used as the initial value, and the object bound to the reference is used as the initial value.

4) A non-constant reference cannot be bound with a nominal value or expression.

15. pointer

1) the pointer itself is an object that allows assignment and copying, and can point to different objects successively in the life cycle.

2) the initial value does not need to be assigned during definition. If the block scope definition is not initialized, its value is uncertain.

3) The reference is not an object and has no actual address. You cannot define a pointer to the reference.

In 16. C ++ 11, it is best to use nullptr to initialize a null pointer. The int variable cannot be directly assigned to the pointer. The following code is incorrect.

1 int zero = 0; 2 int * p = zero; // error!

17. void * pointer, which can store any type of pointer, but cannot access the objects stored in the memory space. To obtain its content, you can use forced type conversion.

1 int a = 1; 2 void * pi = & a; 3 cout <* pi <'\ n'; // error, the value 4 int * pa = static_cast <int *> (pi); 5 cout <* pa <'\ n'; // output 1 successfully

18. Pointer Reference

1 int I = 1; 2 int * pi = & I; 3 int * & ri = p; // reference to pointer p

You can understand this.

1) When we regard & ri as a whole pp, it becomes int * pp = p. We can see that pp is an int pointer pointing to the content pointed to by p.

2) & ri is an int pointer, that is, ri is a reference to an int pointer.

19. const qualifier

1) The value of a const object cannot be changed once it is created, so it must be initialized. However, it does not distinguish between compile and runtime initialization.

2) using one object to initialize another object does not matter if they are const, such

1 int I = 42; 2 const int ci = I; // correct, value copy 3 int j = ci; // correct, value copy

3) by default, the const object is only valid in the file. To use the same const object in multiple files, add the extern keyword,

1 // file_1.h file, define and initialize 2 extern const int bufSize = 512; 3 // file_2.h file, declare the constant 4 extern const int bufSize;

Such variables have three characteristics. 1. Attackers can access external files. 2. Define only once. 3. It cannot be changed.

4) References to const.

4-1. A constant reference can be bound to a very large number of objects, literal faces, or even a general expression, and the type does not have a hard requirement, as long as the type conversion can be successful. Normal references cannot reference the nominal value and expression, and must be of the same type.

Int I = 42; const int & r1 = I; // correct const int & r2 = 42; // correct const int & r3 = r1 * 2; // The correct int & r4 = I; // The correct int & r5 = r1; // The error code returned when the normal int cannot be bound to the int constant int & r6 = 42; // error double j = 1.0; int & r7 = j; // error const int & r8 = j; // correct

4-2. The reference to const can reference a non-const object, but it means that the bound object cannot be modified through this reference, and it does not affect the change of the object through other methods.

5) pointer and const

5-1. The combination of the three common pointers and const is as follows. During analysis, the semantics can be determined based on the proximity principle.

1 const int * p; // pointer to a constant. The underlying const2 int * const p = & pi; // the pointer itself is a constant, and the top-level const, 3 const int * const p = & pi; // a constant pointer to a constant. The base-level const and the top-level const must also be initialized.

5-2. When the copy operation is executed, the top-level const has no effect, and whether the objects copied and imported by the merge operation are constants has no effect. However, the limitations of the underlying const cannot be ignored. constants can be converted into constants, and vice versa.

5-3. constant expression, which does not change and the result is known during compilation. In the new C ++ 11 standard, the constant expression is declared as the constexpr type, and the compiler will verify whether the variable is a constant expression.

20. The type alias makes the complex type name simple and easy to understand. The following two methods can be used:

1 typedef double wages, * p; // wages is a synonym for double, and p is a synonym for double *. 2 using SI = Sales_item; // SI is a synonym for Sales_item.

Note: If the type alias is consistent with the type or constant, you cannot replace the type alias with the original type in the segmentation semantics. Instead, you can regard the type alias as a basic data type. You can use pstring as the basic type of the int type. First, explain the outer part, and then explain pstring itself. As follows:

1 typedef char * pstring; 2 const pstring cstr = 0; // cstr is a constant pointer to char 3 const pstring * ps; // ps is a pointer, the object is a constant pointer to char. 4 // two Interpretation Schemes of the Code in line with 2nd; 5 const (char) * cstr; // error. The basic type is char, interpreted as the pointer to const char 6 const (char *) cstr; // correct, basic type: char *, interpreted as a constant pointer to char

21. Description of the auto type. The Compiler analyzes the type of the expression for us and must have an initial value.

1) one statement can declare multiple variables, but the initial basic data types of all variables should be the same, because one declaration statement can only have one basic type. For example

1 auto I = 0, * p = & I; // correct 2 auto sz = 0, pi = 3.14; // The type of sz and pi is inconsistent

2) When the compiler performs auto type inference, its value and initial value type are not exactly the same.

① When reference is used as the initial value, the referenced value is actually involved in initialization.

② Auto ignores the top-level const, while the bottom-level const is retained.

③ If you want to infer that the auto type is a top-level const, you must add the const modifier.

④ When auto type is set to reference, the top-level const in the initial value is still retained.

1 int I = 0; 2 const int ci = I, & cr = ci; 3 auto B = ci; // B is an integer 4 auto c = cr; // c is the integer 5 auto e = & ci; // e is the pointer to the integer constant 6 const auto f = ci; // The deduction type of ci is int, f is const int7 auto & g = ci; // g is an integer constant reference 8 auto & h = 42; // error, cannot bind a literal value 9 const auto & j = 42 for a very large volume reference; // correct

22. decltype type indicator. You only want to deduce the type of the variable to be defined and do not want to use the value of this expression to initialize the variable. It differs from auto in type inference,

1) decltype includes the top-level const and reference and returns the type of the variable.

2) When the expression can be used as the left value or added with a layer or multilayer brackets, the corresponding type of reference is reversed. However, the brackets seem to have only an effect on the variables, but do not affect the expressions.

1 const int ci = 0, & cj = ci; 2 decltype (ci) x = 0; // The type of x is const int 3 decltype (cj) y = x; // The type of y is const int &, which must be 4 int I = 42, * p = & I, & r = I; 5 decltype (I); // a is int 6 decltype (I) B = I; // B is int &, must initialize 7 decltype (r) c = I; // c is int & and must initialize 8 decltype (r + 0) d; // d is int 9 decltype (r + 0) e; // e is int10 decltype (* p) f = I; // f is int & and must be initialized

23. C ++ 11 new standards stipulate that an internal class initial value can be provided for data members in the class. Parentheses are not allowed when values are assigned. We recommend that you use curly brackets.

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.