C language programming - forced type conversion

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

(type_name) expression

Consider the following example, using the cast operator to divide an integer variable by another integer variable to obtain a floating point number:


#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 produces the following result:

Value of mean : 3.400000

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

Type conversions can be implicit, performed automatically by the compiler, or explicit, specified by using the cast operator . When programming, it is a good programming practice to use the cast operator whenever type conversion is required.

integer promotion

Integer promotion refers to the process of converting an integer type smaller than int or unsigned int to int or unsigned int . See the example below, adding a character to an int:


#include <stdio.h>
 
int main()
{
   int  i = 17;
   char c = 'c'; /* ascii 值是 99 */
   int sum;
 
   sum = i + c;
   printf("Value of sum : %d\n", sum );
 
}

When the above code is compiled and executed, it produces the following result:

Value of sum : 116

Here, the value of sum is 116 because the compiler does integer promotions to convert the value of 'c' to the corresponding ascii value when performing the actual addition.

Common Arithmetic Conversions

Commonly used arithmetic conversions implicitly coerce values ​​to the same type. The compiler performs integer promotions first , and if the operands are of different types, they are converted to the highest-level type occurring in the following hierarchy:

 

The usual arithmetic conversions do not apply to the assignment operator, logical operators && and ||. Let's see the following example to understand the concept:


#include <stdio.h>
 
int main()
{
   int  i = 17;
   char c = 'c'; /* ascii 值是 99 */
   float sum;
 
   sum = i + c;
   printf("Value of sum : %f\n", sum );
 
}

When the above code is compiled and executed, it produces the following result:

Value of sum : 116.000000

Here, c is first converted to an integer, but since the final value is of type float, the usual arithmetic conversions are applied, and the compiler converts i and c to float and adds them to get a float .

If the operands on both sides of an operator are of different types, they must first be converted to the same type, that is, the lower type is converted to a higher type, and then participate in the operation. The conversion rules are shown in the figure below.

 

Guess you like

Origin blog.csdn.net/shiwei0813/article/details/131295414