Curriculum
Course: C basic
Login

Curriculum

C basic

C Introduction

0/1

C Get Started

0/1

C Comments

0/1

C Constants

0/1

C Operators

0/1

C Break and Continue

0/1

C User Input

0/1

C Memory Address

0/1

C Structures

0/1
Text lesson

Type Conversion

Type Conversion

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:

Example

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:

  1. Implicit Conversion (automatically)
  2. Explicit Conversion (manually)

Implicit 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:

Example

// 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:

Example

// 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?

Example

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

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:

Example

// 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.

Example

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.

Example

int num1 = 5;
int num2 = 2;
float sum = (float) num1 / num2;

printf(“%.1f”, sum); // 2.5 

Real-Life Example

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:

Example

// Set the maximum possible score in the game to 500
int maxScore = 500;

// The actual score of the user
int userScore = 420;

/* Calculate the percantage of the user’s score in relation to the maximum available score.
Convert userScore to float to make sure that the division is accurate */

float percentage = (float) userScore / maxScore * 100.0;

// Print the percentage
printf(“User’s percentage is %.2f”, percentage);