- Type conversion
See the document: http://www.cnblogs.com/hoys/archive/2012/04/09/2438718.html
Automatic conversionFollow the following rules:
1) If the type of computation involved is different, it is first converted to the same type and then calculated.
2) The transformation is carried out in the direction of increasing data length to ensure that the accuracy is not reduced. For example, when int and long are operated, transform int to long and then calculate.
a.If the number of bytes of two types is different, it is converted to a high byte type.
b.If two types of bytes are the same, and one is signed and one is unsigned, it is converted to an unsigned type.
3) All floating-point operations are carried out in double precision, even if the expressions containing only float single precision operations are first converted to double type and then calculated.
4) charType and short type must be converted into int type when they participate in operation.
5) In assignment operation, the type of the right value of the assignment number will be converted to the type of the left quantity when the data type of the two sides of the assignment number is different. If the data type length on the right side is long on the left side, some data will be lost, which will reduce the accuracy. The missing part will be rounded off by four rounds of five entries.
There are three types of implicit type conversion, that is,Arithmetic conversion、Assignment conversionandOutput conversion。
In arithmetic operations (plus, subtraction, multiplication, division, redundancy, and symbol operation), different types of numbers must be converted into the same type of data to operate, and the arithmetic conversion principle is:
When calculating operations, the other types of bits are converted to the type based on the longest type in the expression, such as:
(1)If there are double or float types in the operation number, all other types of data are converted into double types for operation.
(2)If the longest type of operation number is long type, all other type numbers are converted to long type numbers.
(3)If the longest type in the arithmetic number is int type, then the char type is also converted to int type for operation. Arithmetic conversion is done automatically in the process of computation.
When an assignment is performed, the data type on the right of the assignment operator must be converted to the type left of the assignment number, and if the length of the data type on the right is greater than that on the left, it is truncated or rounded.
The following is illustrated by an example:
(1)First, we calculate ch/i, ch to int, ch/i to int.
(2)Then f*d-i is calculated, because the longest type is double type, so f to double, I to double, f*d-i to double.
(3)(ch/i) And (f*d-i) add operation, because f*d-i is double type, so ch/i to double, ch/i+ (f*d-i) to double type.
(4)Because result is int, ch/i+ (f*d-i) to double to int, that is, truncation and rounding, and finally the integer value.
When the data is output in a specified format with the printf function in the program, the type conversion is automatically converted when the type of salt data to be output is not in conformity with the output format, such as when a long type data is output with an integer format (%d), it is equivalent to converting the long type into an integer (int) number according to the output.The output of a character (char) data in an integer format is equivalent to converting the char type to the int type output.
Note: when the long data is converted into short data output, its value can not exceed the allowable value range of the short data, otherwise it will be wrong when converting. Such as:
The run result is 14464, because the maximum value allowed by the int type is 3276780000 beyond this value, so the result is taken as the remainder of the 32768 mode, that is to do the following operation as follows:
Output data types often do not match the output format, such as:
There will be a wrong result.
If the same sentence or expression uses a variety of variables and constants (type mixing), C automatically converts them into the same type. The following are the basic rules of automatic type conversion:
1. In the expression, the values of the char and short types, whether symbolic or unsigned, are automatically converted to int or unsigned int (if short is the same size as int, unsigned short)The scope of the representation is greater than int, in which case the unsigned short is converted to unsigned int. Because they are converted to a larger type, conversion is called promotion.
2. According to the order from high to low, the data types are classified into long double, double, float, unsigned long long, long long, unsigned long, and unsigned.Ng, unsigned int and int. Here is a small exception. If long and int are of the same size, the rank of unsigned int should be above long. Char and short did not come outNow in this rank list, it is because they should have been upgraded to int or unsigned int.
3. In any operation involving two data types, a lower level of type between them will be converted to a higher level type.
4. In the assignment statement, = the value on the right before transforming the data on the left side, we first convert the data type of the right value to the type of the left variable. That is to say, what data type is the variable on the left, and what data type is the value on the right. This process may lead to the class of values on the rightType upgrade may also lead to its type reduction (demotion). The so-called “downgrade” means that the higher rank type is converted to a lower rank type.
5. When passed to a function as a parameter, char and short will be converted to int, and float will be converted to double. Using function prototypes can avoid this automatic upgrade.
//Function prototype is similar to function head in function definition, also known as function head.Function declaration。
Note: there are many pitfalls and hidden dangers in this project.
- Negative number:
It is in the computerComplementThe form exists.
-》On the complement code:
FOR：The positive number is the complement itself
FOR：Negative number “complement = original code (binary) -> inverse -> add 1
Example: the original code of -7 (10000111) –Bit by bit（11111000）（Negative symbol bit constant）→Plus 1(11111001)
- In displacement operation
>0 > >.
<0 > > Supplement 1
<< It’s all 0.