MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

Type conversion

Mixed-mode arithmetic with integers and real numbers
"In the previous chapters, we have performed calculations involving integers with integers and real numbers with real numbers."
I have never performed calculations involving a mixture of integers and real numbers.

Mixed calculation with integers and real numbers refers to calculations such as 1.03 × 9.
This calculation involves the multiplication of a real number by an integer.
What would the answer be in a case like this?

To conclude, in this case, the result will be a real number.
In C, when calculating with integers and floating-point numbers, the result is converted to a floating-point number.
This is because converting the result to an integer would eliminate the real part.

The following program is an example of actually calculating 1.03 x 9.

Source code
#include <stdio.h>

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

The results of this program's execution are as follows:

Execution results
9.270000

I think you can see that the answer has been converted to a real number.
I also tried displaying it as an integer using the %d specifier, and the result was as follows.

Execution results
-10486

It's clear that the values are incorrect.
Please note that the appearance of this display may vary depending on your computer environment and may not be exactly the same.
Forced conversion
In calculations involving integers and real numbers, we have found that the answer is a real number.
However, there may be instances where an integer answer is more convenient.

For example, the formula for calculating consumption tax is amount x 1.05 (as of September 14, 2004),
"Normally, performing this calculation results in a real number as the answer."
It's unusual for amounts to include decimals.

If there were a way to convert a real number to an integer, this problem would be solved.
C has a feature called casting for forcibly converting data types.

キーワード
【Type casting】

Forceful type conversion


Here's how to use type casting.

Type casting
(Type name for conversion) numeric or variable name

For example, if you want to convert a real number like 1.05 to an integer, you can do so by using (int)1.05.
1.05 is converted to an integer and treated as 1.

If you use this method, you can also calculate consumption tax.
The following program is an example of calculating the consumption tax for a 360 yen item.

Source code
#include <stdio.h>

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

The results of this program's execution are as follows:

Execution results
378

"The reason this program is putting parentheses around 1.05 x 360 is..."
"This is because without parentheses, 1.05 would be evaluated first and become 1."
Regardless of this specific example, the order of operations is important to reduce computer-specific calculation errors.

Furthermore, the results of real number calculations and their converted integer values may vary depending on the compiler and computer type.
There might be slight variations, so it won't always be exactly 378.

Casting can, of course, also be used with variables, and the usage is the same.


About This Site

Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.

Part 0: Program Overview
  1. What is a program?
Chapter 3: Displaying on the Screen
  1. String Display
  2. newline character
  3. Practice Problem 3
Chapter 4: Displaying and Calculating Numbers
  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4
Chapter 6: Input from the Keyboard
  1. input function
  2. The fear of input
  3. Practice Problem 6
Chapter 9: Repeating a Fixed Number of Times
  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9
Chapter 10: Repeating Without Knowing the Number of Times
  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10
Chapter 13: Handling Multiple Variables at Once
  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13
Chapter 19: Dynamic Arrays
  1. Create arrays freely.
  2. Practice Problem 19