At times, it’s necessary to convert the value of one data type to another, a process known as type conversion.
For instance, when dividing two integers, like 5 by 2, the expected result is 2.5. However, given that we’re dealing with integers (rather than floating-point values),
the following example will simply output 2:
int x = 5; int y = 2; int sum = 5 / 2; printf(“%d”, sum); // Outputs 2 |
To achieve the correct outcome, understanding how type conversion operates is essential.
In C, there are two types of conversion:
Implicit conversion occurs automatically by the compiler when assigning a value of one type to another.
For instance, when assigning an int value to a float type:
// Automatic conversion: int to float float myFloat = 9; printf(“%f”, myFloat); // 9.000000 |
As demonstrated, the compiler automatically converts the integer value 9 to a float value of 9.000000.
However, this automatic conversion can pose a risk, potentially leading to loss of precision in certain scenarios.
Particularly noteworthy is the reverse scenario, where the following example automatically converts the float value 9.99 to
an integer value of 9:
// Automatic conversion: float to int int myInt = 9.99; printf(“%d”, myInt); // 9 |
What happened to the decimal part, .99 ? This data might be crucial for our program! Hence, exercising caution is paramount.
It’s essential to comprehend how the compiler operates in such scenarios to prevent unexpected outcomes.
As another illustration, when dividing two integers, such as 5 by 2, the quotient is expected to be 2.5.
However, as discussed earlier, if you store the result as an integer, it will display only the whole number, 2.
Therefore, it would be advisable to store the result as a float or a double to retain the decimal part, correct?
float sum = 5 / 2;
printf(“%f”, sum); // 2.000000 |
Why does the result appear as 2.00000 rather than 2.5? This occurs because both 5 and 2 are integers involved in the division operation. In such cases, you must explicitly convert the integer values to floating-point values. (See below for an example).
Explicit conversion is conducted manually by encapsulating the desired type within parentheses () preceding the value.
In light of the issue presented in the preceding example, we can now obtain the correct result:
// Manual conversion: int to float float sum = (float) 5 / 2; printf(“%f”, sum); // 2.500000 |
Another method is to prepend the desired type directly before the variable.
int num1 = 5; int num2 = 2; float sum = (float) num1 / num2; printf(“%f”, sum); // 2.500000 |
Building upon your knowledge of “decimal precision” from the previous chapter, you can enhance the output by eliminating any extraneous zeros if desired.
int num1 = 5; int num2 = 2; float sum = (float) num1 / num2; printf(“%.1f”, sum); // 2.5 |
Here’s a practical example demonstrating data types and type conversion. We’ll create a program to compute the percentage of a user’s score relative to the maximum score in a game:
// Set the maximum possible score in the game to 500 int maxScore = 500; // The actual score of the user /* Calculate the percantage of the user’s score in relation to the maximum available score. // Print the percentage |