C/C++ programming knowledge-forced type conversion

Coercion is the conversion of a variable from one type to another data type. For example, if you want to store a long type value into a simple integer type, you need to cast the long type to an int type. You can use the cast operator to explicitly convert a value from one type to another, as shown below:

(type_name) expression

Please see the following example, use the cast operator to divide an integer variable by another integer variable to get a floating point number:

Examples:

#include <stdio.h>

int main ()

{

  int sum = 17, count = 5;

  double mean;

  mean = (double) sum / count;

  printf("Value of mean : %f\n", mean );

}

When the above code is compiled and executed, it will produce the following results:

Value of mean :3.400000

It should be noted here that the precedence of the cast operator is greater than the division, so the value of sum is first converted to double , and then divided by count to get a value of type double.

Type conversion can be implicit, performed automatically by the compiler, or explicit, specified by using a cast operator . In programming, it is a good programming practice to use the coercive type conversion operator when there is a need for type conversion.

Integer promotion

Integer promotion refers to  the process of converting an integer type smaller than int or unsigned int to int or unsigned int . Please see the example below to add a character to int:

Examples:

#include <stdio.h>

int main ()

{

  int  i = 17;

  char c ='c'; /* ascii value is 99 */

  int I;

  sum = i + c;

  printf("Value of sum : %d\n", sum );

}

When the above code is compiled and executed, it will produce the following results:

Value of sum :116

Here, the value of sum is 116, because the compiler has performed integer promotion. When performing the actual addition operation, the value of'c' is converted into the corresponding ascii value.

Common arithmetic conversion

A common arithmetic conversion is to implicitly cast a value to the same type. The compiler first performs integer promotion . If the operands are of different types, they will be converted to the highest type in the following levels:

Commonly used arithmetic conversions do not apply to assignment operators, logical operators && and ||. Let us look at the following example to understand this concept:

#include <stdio.h>

int main ()

{

  int  i = 17;

  char c ='c'; /* ascii value is 99 */

  float sum;

  sum = i + c;

  printf("Value of sum : %f\n", sum );

}

When the above code is compiled and executed, it will produce the following results:

Value of sum :116.000000

Here, c is first converted to an integer, but since the final value is of type float, common arithmetic conversions will be applied. The compiler will convert i and c to floating point and add them to get a floating point number .

Want to be a programmer, want to master programming, follow the editor

Free study books:

Free learning materials:

Guess you like

Origin blog.csdn.net/weixin_45713725/article/details/113111852