Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - C language rules for implicit conversion of numerical types. Please help me see if I understand correctly.
C language rules for implicit conversion of numerical types. Please help me see if I understand correctly.

This is a good question. It is a complex issue. We can only discuss it together based on your understanding. If you have different opinions

You can come to 0x30 Baidu Tieba to post directly to communicate with us. Baidu knows that if you are not there often, you may not receive follow-up prompts in time.

1. When converting an integer to an integer, the lower bits of the data bits will be stored first, and the overflowed bits will be discarded. //Not necessarily

Reason: There are two situations in which small-byte integers are converted to large numbers. The integer number of bytes depends on whether the little byte is a signed number or an unsigned number. For example, the following code:

unsigned char ux = 0x80;

char x = 128;

int i = ux; //x is unsigned at this time char, so the value of i is 128

i = x; //At this time, x is a signed char, so the value of i is -128

The essence is to use signed symbols Extension, unsigned uses 0 extension.

Converting a large-byte integer to a small-byte integer depends on the storage order of the CPU. What you are talking about is only on the Intel platform, which is a little endian machine, for example:

int i = 0x12345678;

char x = i; //On a little endian machine, Its value is 0x78 as you said, but on a big endian machine it will be 0x12.

2. When converting floating point to integer, the high bits of the data bits (bits before the decimal point) will be stored first, and the bits after the decimal point will be discarded. //You can set the discarding method

Reason: Floating point number When converting to an integer, C/C++ directly discards the decimal part. For example

int i;

for (double f=1.0; (2.0 -f) >=FLT_EPSILON ; f+=0.1) {

i = f;

printf("%d\n", i); //Whether it is 1.0 or 1.9, it will be converted into an integer and it will always be 1

}

But this This discarding rule can be modified by calling floor() or ceil() declared in math.h. The former is downward integerization, and the latter is upward integerization. In other words, for example, 1.5 floor(1.5 ) Its value is 1

(downward integerization), ceil(1.5) is 2.0, which is upward integerization.

3. When converting from floating point to floating point, the high bits of the data bits will be stored first, and those that overflow will be discarded. (When converting from high precision to low precision, rounding will be performed)

High The C/C++ standard does not explain the conversion from precision to low precision. It is an implementation decision. In vc++, rounding is performed. This conversion actually makes no sense because the data is lost.

To convert low precision to high precision, this can be directly improved.

4. When converting an integer to a floating point, the lower bits of the data will be stored first, and the overflowed bits will be discarded.

This C/C++ standard has not explained it, and it is an implementation decision.