Implicit (automatic) data type conversion in c language

Reprinted from   https://blog.csdn.net/u010815257/article/details/50178499

1. Conversion rules in arithmetic operators:

double ←── float 高

long

unsigned

int ←── char,short 低

Note: The horizontal arrow in the figure indicates the necessary conversion. For example, if two float types are involved in the operation, although they are of the same type, they still need to be converted into double type before operation, and the result is also double type. Vertical arrows indicate when the operations on either side of the operator

When the arithmetic is converted into different types, for example, a long type data and an int type data are operated together, it is necessary to convert the int type data to the long type first, and then perform the operation on the two, and the result is the long type. All these conversions are done automatically by the system

When you use it, you only need to know the type of the result from it. These conversions can be said to be automatic.

2. Conversion rules in assignment operators:

In the assignment operation, when the data types of the quantities on both sides of the assignment number are different, the type of the quantity on the right side of the assignment number will be converted to the type of the quantity on the left side. If the length of the data type on the right is long on the left, part of the data will be lost, which will reduce the precision. 

Missing parts are rounded up by rounding up.

Note: Default conversion types for constants:

1. The default data type of integer constants is int type, that is, signed integer type.

2. The default type of floating point constants is double type, that is, long floating point type.

3. Conversion during output:

When the data is output in the specified format with the printf function in the program, when the type of the salt data to be output does not match the output format, the type conversion will be performed automatically. For example, when a long type data is output in the integer format (%d), then Equivalent to converting the long type

Change to integer (int) data output; when a character (char) type data is output in integer format, it is equivalent to converting char type to int type output.

Note: When converting longer data into short data output, its value cannot exceed the allowable value range of short data, otherwise an error will occur during conversion.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324519760&siteId=291194637