Different types of data are automatically converted from left to right (from low to high) in relation to the following.
If these data types are mixed, the different types of data in the operation are first converted to the same type, and then the conversion is done automatically from left to right, as shown in table 2-3.
Table 2-3 sequence table of type conversions
| Operand 1 type |
Operand 2 Type |
Converted Type |
| _bool, char, short int, enum type |
Int |
Int |
| _bool, char, short int, enum type, int |
Long int |
Long int |
| _bool, char, short int, enum type, int, long int |
Long Long |
Long Long |
| _bool, char, short int, enum type, int, long int, long long |
Float |
Float |
| _bool, char, short int, enum type, int, long int, long long, float |
Double |
Double |
| _bool, char, short int, enum type, int, long int, long long, float, double |
Long double |
Long double |
If you have the following representation, where F is the float type, I is the int type, L is a long int type, S is a short int type, what is the result type?
f * i + l/s
If you follow a type conversion that is right to left, you need to force type conversions, and the coercion type conversion syntax is simple in that it is preceded by the data (the target type), but the conversion is risky and may result in loss of data that needs to be done cautiously. For example:
long int l = 6666666666;
NSLog (@ "L =%li", l);
int i = (int) l;
NSLog (@ "i =%i", i);
When a cast is embedded in another expression, it is intertwined with the precedence of the operation, and the situation becomes more complex, assuming that there are several statements:
int total = 3446;
int n = 6;
float average = total/n;
The variable that runs the float average result is 574.333, this data is more accurate than the above calculation, because (float) total Converts the total variable of type int to the total variable of float type.
This article is from the "Dongsheng" blog, please be sure to keep this source http://2009315319.blog.51cto.com/701759/716454