Type conversion is the process of converting a value from one data type to another. The C compiler can perform conversions automatically when assigning values to variables or evaluating expressions. For example, assigning an integer to a floating-point variable converts the integer to a floating-point value.
Type conversion in C can be classified into two categories: implicit type conversion and explicit type conversion.
Implicit Type Conversion
Implicit, or automatic, type conversion occurs when the compiler converts a value from one data type to another without requiring the programmer to specify a cast.
During arithmetic operations, the usual arithmetic conversions determine the types used in the calculation. When assigning a value to a variable, the value is converted to the variable’s type.
These conversions can sometimes change a value or lose information. For example, converting a negative signed integer to an unsigned integer produces a value according to the unsigned type’s range. Converting a floating-point value to an integer discards its fractional part.
// An example of implicit conversion
int main()
{
int x = -5; // integer x
float z = x; // x is implicitly converted to float;
char c=x+40;// x is implicitly converted to the char character #;
unsigned int y=x;// x is implicitly converted to unsigned int with loss of data;
printf("z = %f", z);
printf("\n");
printf("c = %c", c);
printf("\n");
printf("y = %u", y);
return 0;
}
Output on a typical system using 8-bit bytes and ASCII characters:
z = -5.000000
c = #
y = 4294967291
In this example:
- float z = x; converts the integer -5 to a floating-point value.
- char c = x + 40; calculates -5 + 40, producing 35, which represents # in ASCII.
- unsigned int y = x; converts -5 to the corresponding unsigned value. For a 32-bit unsigned int, this is 4294967291.
The precise output depends on the implementation’s type sizes and character encoding.
Explicit Type Conversion
Explicit type conversion, also known as type casting, occurs when the programmer specifies the desired type using a cast operator.
A cast consists of a type name enclosed in parentheses, placed before the expression to be converted. The syntax is:
(type_name) expression
An explicit cast is useful when you want to control how an expression is evaluated. For example, integer division discards the fractional part of the result. Converting one operand to float before division produces a floating-point result.
#include <stdio.h>
int main(void)
{
float a;
int b = 2, c = 3;
a = (float)b / (float)c;
printf("%f\n", a);
return 0;
}
Output:
0.666667
In this example:
- (float)b converts b from an integer to a floating-point value.
- (float)c converts c to a floating-point value.
- The division is performed using floating-point arithmetic, preserving the fractional part of the result.
Without the casts, b / c would perform integer division and produce 0. Assigning that result to a would give 0.000000, because the fractional part would already have been discarded.
Important: A cast does not guarantee that a conversion is safe or preserves the original value. The result depends on the source and destination types.