当前位置:网站首页>C language data type conversion rules (implicit conversion + explicit conversion)

C language data type conversion rules (implicit conversion + explicit conversion)

2022-06-22 05:07:00 Jiangxueqian

Type conversion during assignment

If the types on both sides of the assignment operator are inconsistent , But when they are all basic types , Type conversion is required during assignment . Type conversion is performed automatically by the system , The rule of transformation is :

  1. Convert floating point data ( Including single 、 Double precision ) When you assign an integer variable , First round the floating-point number , That is, the decimal part is discarded , Then give an integer variable .
  2. Assign integer data to a single 、 Double precision variable , Constant value , But it's stored in a variable as a floating point number .
  3. Will a double Type data is assigned to float variable , First convert double precision numbers to single precision numbers , That is to say, only 6~7 Significant digits , Store in float Type variable 4 In bytes . It should be noted that the size of the double precision value cannot exceed float The range of values for a variable of type .
  4. When character data is assigned to an integer variable , The ASCII The code value is assigned to an integer variable .
  5. Assign an integer data with more bytes to an integer variable or character variable with less bytes ( For example, occupy 4 Bytes of int Type data is assigned to account for 2 Bytes of short Variable or percentage 1 Bytes of char Variable ) when , Only send its low byte intact to the assigned variable ( That is, truncation occurs ).

——《C Programming ( The fifth edition )》61 page


Implicit type conversion

  1. When it appears in an expression , Signed and unsigned char and short Will be automatically converted to int Participate in operation . When short And int When the size is the same ,unsigned short Will be converted into unsigned int.
  2. In any operation that contains two data types , Both values are converted to the higher level of the two types ( be called “ promote ”), Then participate in the calculation , Only data of the same type can be directly calculated .
    1. The order of type level from high to low is :long double 、double、float、unsigned long long、long long、 usinged long、long、unsigned int、int.
    2. When long and int Have the same size , here unsigned int Than long A higher level . The reason why... Is not involved char and short , Because they have been elevated to int perhaps unsigned int.
    3. Examples demonstrate
3 + 4 / 5.0f + 6 - 9.0(double)
 According to operator priority , To calculate  4 / 5.0f,  So first put  4  convert to  float 4.0f  The calculation result is  0.8f

3 + 0.8f + 6 - 9.0
 Then according to the order from left to right :
 First calculate  3 + 0.8f, 3  convert to  3.0f  The calculation result is  3.8f
 Calculate again 3.8f + 6, 6  Convert to  6.0f  The calculation result is  9.8f

9.8f - 9.0
9.8f  Convert to  double 9.8  The calculation result is  18.8(double)
        
 Not all of them are converted to double        
  1. In the assignment statement , The final result of the calculation will be converted to the type of the variable to the left of the assignment operator , This process may be upgraded , May also be degraded , Demotion is converting a value to a lower type .
int a = 2022.22; // double turn int, Downgrade 
double b = 22;   // int turn double, upgrade 
  1. When passed as an argument to a function ,char and short Will be automatically converted to int,float Will be converted into double, Function prototypes can be used to prevent elevation .
  2. Ascension is usually a smooth, lossless process , But downgrades can lead to “ truncation ”.

——《C Primer Plus( The fifth edition )》105 page


Explicit type conversion ( Cast )

form :(type) data / expression

22 + 22.22      // Implicit conversion , The result is double type 
22 + (int)11.11 // I will put 11.11 convert to 11 And again 22 Add up , The result is int type 
原网站

版权声明
本文为[Jiangxueqian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220503534444.html