Type Conversion
Sometimes, you have to convert the value of one data type to another type. This is known as C Type Conversion.
For example, if you try to divide two integers, 5
by 2
, you would expect the result to be 2.5
. But since we are working with integers (and not floating-point values), the following example will just output 2
:
Example:
There are two types of conversion in C:
-
Implicit Conversion (automatically)
-
Explicit Conversion (manually)
Implicit Conversion
Implicit conversion is done automatically by the compiler when you assign a value of one type to another.
For example, if you assign an int
value to a float
type:
Example:
As you can see, the compiler automatically converts the int value 9
to a float value of 9.000000
.
Warning
Especially if it was the other way around - the following example automatically converts the float value
9.99
to an int value of9
:x
Strange Case:
As another example, if you divide two integers: 5
by 2
, you know that the sum is 2.5
. And as you know from the beginning of this page, if you store the sum as an integer, the result will only display the number 2
. Therefore, it would be better to store the sum as a float
or a double
, right?
Example:
Why is the result 2.00000
and not 2.5
? Well, it is because 5 and 2 are still integers in the division. In this case, you need to manually convert the integer values to floating-point values. (see below).
Explicit Conversion (casting)
Explicit conversion is done manually by placing the type in parentheses ()
in front of the value.
Considering our problem from the example above, we can now get the right result:
You can also place the type in front of a variable: