Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - God of @C: Assign an integer to a variable. How should the program be written? An explanation and an example are enough.
God of @C: Assign an integer to a variable. How should the program be written? An explanation and an example are enough.
This involves type conversion, float->; Type int, the system will intercept the integer part of floating-point variable and assign it to int variable.

If multiple types of variables and constants (mixed types) are used in the same statement or expression, C will automatically convert them to the same type. The following are the basic rules for automatic type conversion:

1. In the expression, the values of char and short, whether signed or unsigned, are automatically converted to int or unsigned int (if the size of short is the same as that of int, the representation range of unsigned short is larger than that of int, in this case, unsigned short is converted to unsigned int). Because they are converted into types representing a wider range, this conversion is called "promotion".

2. Sort various data types from high to low, namely: long double, double, float, unsigned long long, long long, unsigned long, long, unsigned int and int. There is a small exception here. If long and int are the same size, then the rank of unsigned int should be above long. Char and short do not appear in this ranking list because they should have been upgraded to int or unsigned int.

3. In any operation involving two data types, the lower-level type will be converted to the higher-level type.

4. In the assignment statement, before assigning the value on the right of = to the variable on the left of =, the data type of the right value should be converted to the type of the variable on the left. That is, the value on the right will be converted to the value of any data type of the variable on the left. This process may cause the type of the value on the right to be upgraded or downgraded. The so-called "demotion" is to convert a higher-ranking type into a lower-ranking type.

5. When passed as parameters to the function, char and short will be converted into int and float will be converted into double. Using function prototypes can avoid this automatic upgrade.

I hope I can help you, and I hope to adopt it!