Type conversion, also known as type casting, is the process of converting a variable's value from one data type to another. For example, converting an int
to a float
. C supports two primary types of conversion:
1. Implicit Type Conversion: Performed automatically by the compiler.
2. Explicit Type Conversion: Performed manually by the programmer.
1. Implicit Type Conversion (Coercion) ⚙️
Implicit conversion is an automatic conversion performed by the compiler when you mix different data types in an expression. The compiler generally converts the "smaller" or "lower-ranking" type to the "larger" or "higher-ranking" type to avoid any loss of data. This is also called type promotion.
Type Promotion Hierarchy
The compiler follows a set of rules to determine how to promote types. The general hierarchy from highest to lowest rank is:
long double > double > float > unsigned long
> long > unsigned int > int
When an operation involves two different types, the lower-ranking type is promoted to the higher-ranking type.
Example
In this example,
the int
variable num_int
is automatically converted to a float
before the addition operation takes place. The result
is therefore a float
.
C
#include <stdio.h>
int main() {
int num_int =
10;
float num_float =
5.5f;
float sum;
// 'num_int' is implicitly converted to a float (10.0f)
// Then 10.0f is added to 5.5f
sum = num_int + num_float;
printf(
"The integer value is: %d\n", num_int);
printf(
"The float value is: %f\n", num_float);
printf(
"The sum (as a float) is: %f\n", sum);
return
0;
}
size=2 width="100%" align=center>
2. Explicit Type Conversion (Casting) 🖐️
Explicit
conversion is a manual conversion
where the programmer forces a value to be converted to another type using the cast
operator (type)
. This is often used when you need to control the
conversion precisely, especially in cases where data loss might occur.
·
Syntax: (target_data_type) expression;
While powerful, explicit casting can be risky as it can lead to a loss of information or data truncation.
Example 1: Data Truncation
When a floating-point value is cast to an integer, the fractional part is discarded (truncated).
C
#include <stdio.h>
int main() {
double pi =
3.14159;
int int_pi;
// Explicitly casting the double to an int
// The fractional part (.14159) is lost
int_pi = (
int)pi;
printf(
"Original double value: %lf\n", pi);
printf(
"Value after casting to int: %d\n", int_pi);
return
0;
}
Example 2: A Practical Use Case
A very common use for casting is to ensure correct floating-point division between two integers. Without casting, the result of dividing two integers is always an integer.
C
#include <stdio.h>
int main() {
int total_marks =
500;
int obtained_marks =
430;
// Incorrect: Integer division will truncate the result to 0
float percentage_wrong = obtained_marks / total_marks;
// Correct: Cast one of the operands to float to force floating-point division
float percentage_correct = (
float)obtained_marks / total_marks;
printf(
"Incorrect Percentage (integer division): %f\n", percentage_wrong);
printf(
"Correct Percentage (after casting): %f\n", percentage_correct *
100);
return
0;
}
size=2 width="100%" align=center>
Summary: Implicit vs. Explicit ⚖️
Feature |
Implicit Conversion |
Explicit Conversion |
Performed By |
Compiler |
Programmer |
How it's Done |
Automatically |
Using
the |
Risk |
Generally safe (widening conversion) |
Can be unsafe (narrowing conversion), potential for data loss |
Also Known As |
Coercion, Promotion |
Casting |