MMGames Introduction to C C Language Development Environment C language now Useful Apps Contact Us

Automatic version identification


It's easy to tell by looking at it.

Response Time Checker

I can leave my computer on and do it.

Mouse cleaning time

I can leave my computer on and do it.

Mouse cleaning time

C language learned by suffering
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

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

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.

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

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.

About this Site

The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or better than any book on the market.

Part 0: Program Overview
  1. What is the program?
Chapter 2: How to write a program
  1. Writing Rules
  2. Writing conventions
  3. Exercise 2
Chapter 3: Display on Screen
  1. String display
  2. newline character
  3. Exercise 3
Chapter 4: Numeric Display and Calculation
  1. Numeric Display
  2. Basic Calculations
  3. Type of value
  4. Exercise 4
Chapter 5: Numerical Memory and Calculation
  1. Memorize values
  2. Variable Type
  3. Type conversion
  4. Numeric justification
  5. Exercise 5
Chapter 6: Input from the keyboard
  1. Functions for input
  2. Fear of Input
  3. Exercise 6
Chapter 9: Repetition with a fixed number of times
  1. Sentences that repeat themselves
  2. Loop Operation Mechanism
  3. Exercise 9
Chapter 10: Unknown number of repetitions
  1. Loop of unknown frequency
  2. input check
  3. Exercise 10
Chapter 13: Handling Multiple Variables at Once
  1. Multiple variables are handled together.
  2. How to use arrays
  3. Exercise 13
Chapter 19: Dynamic Arrays
  1. Create arrays at will
  2. Exercise 19
Chapter 20: Multiple Source Files
  1. Minimal division
  2. The Stone of Partition
  3. Exercise 20


Open the 💬 comment submission box