1. Automatic type conversion ( when variable types are different on both sides of the operator)
1). Two variable types are automatically converted to the same type (automatically converted according to the parameter type, not direct bit conversion), and The result of the operation is the converted type
2). When a lower type of data is converted to a higher type, it is generally only changed in form, without affecting the substance of the data, while higher types of data may be lost when converted to a lower type.
3). In the case of automatic type conversion, if the original number is an unsigned number, then at the time of expansion, the high-fill is 0, if the number is signed, then the high-level filled with the symbol bit
#include <stdio.h>int main () { int8; if (A >-1) printf ("8>-1\n"); Else printf ("8<=-1\n"); return 0 ;}
The output is 8<-1, clearly assigned to 8 of the variable I, the result is the program to determine the ratio of 1 is also small, what is the matter?
The root of the problem lies in the unsigned of the variable i definition; As we all know, Int/short/char and other types of signed and unsigned, C in the expression of signed and unsigned mixed operations in three cases:
A. The operands are all signed
B. The operands are all unsigned
C. Operands mixed with signed and unsigned
In the first two cases, the same symbol operation is not a problem, but the case C. Involving mixed computations between different symbols it is important to note that the compiler automatically normalizes the operand: As long as there is an unsigned number in the expression, all operands are converted to unsigned numbers , and the operation is performed by the corresponding unsigned operator, The result of the calculation is also an unsigned number (conclusion: the variable is automatically converted when the operation, and the result is the same as the converted type)
In order to prevent accidental conversion, on the one hand to explicitly specify the type, do not use int, char such a lack of hint of neutral notation, this vague expression with more, will instinctively ignore and avoid the symbolic problem; On the other hand to carefully select the unsigned type, do not just because the unsigned number does not have a negative value to use it to represent the quantity, for example, some people like to use unsigned int to define the number of for/while loops, which is very unsafe, accidentally in the loop and negative comparisons, there will be logic error or death cycle; Finally, if you want the unsigned type to participate in the calculation, you can guarantee that the intermediate operand and the result are signed with the coercion type conversion
2. Type conversion in an assignment, when a type conversion is expected to occur when the operand types on either side of the assignment operator are different, the rule is to convert the type of the expression on the right side of the assignment operator to the type of the left variable, and the exact conversion is as follows:
1). Floating-point type and integral type
When you convert a floating-point number (single-double precision) to an integer, the fractional part of the floating-point is discarded and only the integral part is retained
Assigns an integer value to a floating-point variable, with the value unchanged, changing the form to floating point only, that is, with several 0 after the decimal point
Note: The type conversion is actually mandatory when assigning a value
2). Single-and double-precision floating-point type
Because floating-point values in the C language are always represented by double Precision, the following:
Float type data is only extended at the tail plus 0 for the DOUB1E type data to participate in the operation, and then directly assigned
When the DOUB1E data is converted to float type, it is achieved by truncating the mantissa and rounding is done before truncation.
3). char type and int type
When assigning a value of type int to a char variable, it retains only its minimum 8 bits, and the high part discards
When a char value is assigned to an int variable, some compilers handle it regardless of their value size, while others compile the conversion, and if the char data value is greater than 127, it is treated as a negative number. For the user, if the original char data is positive, the conversion is still positive, if the original char value can be positive, then the conversion will still remain the original value, but the internal representation of the data is different
4) int type and long type
When the long data is assigned to an int variable, the low 16-bit value is given to the int variable, and the high 16-bit truncation is discarded (this assumes the int type is two bytes)
When the int data is sent to a long variable, its external values remain unchanged and the internal form changes
5) unsigned integer
When assigning a unsigned data to an integer variable that occupies the same length of storage unit (e.g. Unsigned→int, unsigned long→long,unsigned short→short), the original value is assigned and the internal storage mode is unchanged. But external values can change.
When you assign a non-unsigned integer data to an unsigned variable of the same length, the internal storage form is unchanged, but the external representation is always unsigned
The data in the computer is indicated by the complement, the highest bit of the int is the sign bit, the negative value is 1, and the positive value is 0. If the value of an unsigned number is less than 32768, the highest bit is 0, and the int variable is given positive values; If the unsigned number is greater than or equal to 32768, the highest bit is 1, and the integer variable is given a negative integer value; Conversely, when a negative integer is assigned to the unsigned variable, the resulting unsigned value is a value greater than 32768 (this assumes the int type is two bytes)
The type conversion form of the C language can make people feel less sophisticated and less restrictive, because regardless of the value of the expression, the system automatically converts it to the left variable of the assignment operator, and the data may be different after the transition, which can lead to errors when it is not noticed, which is indeed a disadvantage and has been criticized by many people. But it should not be forgotten that: C is originally designed to replace the assembly language, so the type of transformation is more casual, of course, with coercion type conversion is a good habit, so, at least from the program can see what to do
3. Forcing type conversions
For a high-to-low cast, essentially a truncated operation, only the lower part of the required portion is retained, the rest of the part is thrown away directly
2.PRINTF is what type to print a bunch of 010101010111
3.SCANF type inconsistency can get data but not necessarily correct
C-Type conversions (traps)