Conversion sequence of C basic data types

Reprinted: https://blog.csdn.net/whui19890911/article/details/8678988

Automatic type conversion: Automatically convert the type of the operand with low precision, which means that the range is small, to the type of operand with high precision, which means that the range is large. In order to get a higher precision calculation result.
double ←—— float high 
↑ 
long 
↑ 
unsigned 
↑ 
int ←—— char, short low 
● The horizontal arrow in the figure indicates the necessary conversion . If there is char or short type data in the expression, it will be converted to int type to participate in the operation. For example, two float-type numbers participate in the operation.

    There are float types in expressions, which are all converted into double types to participate in operations. 

The vertical arrows indicate the conversion when the operands on both sides of the operator are of different types. For example, if a long type data is operated with an int type data, the int type data needs to be converted to the long type first, and then the two operations are performed. The result is long type. All these conversions are carried out automatically by the system, and you only need to know the type of result when using it. These conversions can be said to be automatic.

Character data refers to characters represented by ASCII values ​​such as letters, numbers, and various symbols. 0-127 stores the basic ASCII value, and 128-255 is the extended ASCII value. C language allows the use of signed unsignd to modify char type data, but the difference between different definitions can be displayed only when output in the form of integer data.

When the data types of the operands on both sides of the assignment operator are inconsistent, the system will automatically convert the value of the expression on the right of the assignment number to the type of the variable on the left before assigning. details as follows:

(1) Floating point and integer type 
● When converting a floating point number (single and double precision) to an integer, the decimal part of the floating point number will be discarded and only the integer part will be retained. 
Assign an integer value to a floating-point variable, the value remains unchanged, only the form is changed to a floating-point form, that is, with a number of zeros after the decimal point. Note: The type conversion during assignment is actually mandatory. 
(2) Single and double-precision floating-point types
● Since floating-  point values ​​in C language are always expressed in double-precision, the float type data is only added with 0 at the end to extend the doub1e type data to participate in the operation, and then directly assign the value. When doub1e type data is converted to float type, it is realized by truncating the number, and rounding is performed before truncation.
(3) Char type and int type 
● When the int type value is assigned to the char type variable, only the lowest 8 bits are retained, and the high order part is discarded. 
● When a char type value is assigned to an int type variable, some compilers treat it as a positive number regardless of its value, while other compilers treat it as a negative number if the char type data value is greater than 127 during conversion. For users, if the original char type data takes a positive value, it will still be positive after the conversion; if the original char value can be positive or negative, the original value will still be maintained after the conversion, but the internal representation of the data is different .
(4) Int type and 1ong type 
● When assigning long type data to an int type variable, the low 16-bit value is sent to the int type variable, and the high 16-bit value is truncated and discarded. (It is assumed that the int type occupies two bytes). 
When sending int type data to a long type variable, its external value remains unchanged, while the internal form changes. 
(5) Unsigned integer 
● When assigning an unsigned data to an integer variable occupying the same length of storage unit (such as: unsigned→int, unsigned long→long, unsigned short→short), the original value is assigned according to the internal The storage method remains the same, but the external value may change.
● When assigning a non-unsigned integer data to an unsigned variable with the same length, the internal storage format remains unchanged, but the external representation is always unsigned.

Guess you like

Origin blog.csdn.net/modi000/article/details/113859295