﻿ Type Conversions - Learning C Sufferingly
C language learned by suffering

### Type conversion

##### Mixed integer and real number calculations
Up to the previous chapter, we have performed calculations between integers for integers and between real numbers for real numbers.
We have never performed mixed calculations with integers and real numbers.

A mixed computation with integers and real numbers is, for example, a computation such as 1.03 x 9.
In this calculation, real x integer numbers are being computed.
In such a case, what would the answer be?

As it turns out, in this case, the result is a real number.
In C, the result of a calculation with an integer and a real number is converted to a real number.
This is because if the result is an integer, the real number portion is lost.

The following program is an example of an actual calculation of 1.03 x 9.

source code
`````` #include <stdio.h>

int main(void)
{
printf("%f\n", 1.03 * 9);
return 0;
}``````

The result of executing this program will be as follows

Execution Result
9.270000

Indeed, you can see that the answer is converted to a real number.
I also tried displaying it as an integer with the %d specifier, and got the following.

Execution Result
-10486

Obviously, you can see that the values are ludicrous.
Note that the display here will vary depending on the computer environment, so it will not necessarily be the same.
##### Forced conversion
In the calculation of integers and real numbers, we found that the answer is a real number.
In some cases, however, an integer answer may be more convenient.

For example, the sales tax is calculated by the formula: amount x 1.05 (as of 09/14/2004), but
If this calculation is performed normally, the answer would be a real number.
It is unnatural for the amount to include decimals.

If there were a way to convert real numbers to integers, this problem would be solved.
The C language has a function to force type conversion: cast conversion.

Keywords.
##### Cast conversion

Function to force type conversion

The usage of cast conversion is as follows.

Cast conversion
(Type name to be converted) Numeric or variable name

For example, if you want to convert the real value 1.05 to an integer, you can use (int)1.05
1.05 is converted to an integer and treated as 1.

Using this method, sales tax can also be calculated.
The following program is an example of obtaining sales tax on a product costing ¥360.

Source Code
`````` #include <stdio.h>

int main(void)
{
printf("%d\n", (int)(1.05 * 360));
return 0;
}``````

The result of executing this program will be as follows

Execution Result
378

In this program, the 1.05 x 360 with () is calculated as
Without the parentheses, 1.05 would be converted first and become 1.
Not only in this example, but the order of calculations is important to reduce computer-specific calculation errors.

Note that the result of a real number calculation or its conversion to an integer depends on the compiler and the type of computer.
The result may not be exactly 378, since it may differ slightly.

Cast conversions can, of course, be used for variables, and the usage is the same.

Comment
COMMENT

Open the 💬 comment submission box