Data type conversion refers to the process of converting data (variables or expression results) from one type to another.
1. Explicit Type Conversion (Type Casting)
Explicit type conversion, also known as type casting, is a mechanism in the C programming language that allows programmers to convert a value from one data type to another by explicitly specifying the target data type. It involves explicitly specifying the target data type in order to ensure proper interpretation and manipulation of data.
Type casting is typically performed using casting operators.
The general syntax for type casting in C is:
(target_type) expression
For example, if we have an integer variable num
and we want to treat it as a floating-point number for a specific calculation, we can use explicit type casting as follows:
int num = 10;
float result = (float) num / 3;
In this example, (float)
is the casting operator, and it converts the value of num
to a floating-point number before performing the division operation.
Explicit type conversion is useful in situations where the programmer wants to control the data type conversion explicitly to ensure accuracy and desired behavior in their program.
(float) a; // convert a to float type
(int)(x+y); //convert x+y to int type
(float) 100; //convert a constant to float type
Example 1.1 convert a integer to a floating-point number
#include <stdio.h> int main() { int sum = 17, count = 5; double mean; mean = (double) sum / count; printf("Value of mean : %f\n", mean); return 0; }
results:
Value of mean : 3.400000
It is important to note that the type casting operator ( ) has a higher precedence than the division operator (/). In the expression (double) sum / count
, sum
will first be converted to the double
type using type casting, and then the division operation will be performed. If it were written as (double) (sum / count)
, the result would be 3.000000.
Example 1.2 To convert an integer to a floating-point number
#include <stdio.h>
int main()
{
int sum = 17, count = 5;
double mean;
mean = (double)( sum / count);
printf("Value of mean : %f\n", mean);
return 0;
}
Results:
Value of mean : 3.000000
Apart from explicit type casting, the C compiler also performs implicit data type conversion, known as automatic type conversion or implicit type coercion, during mixed-type operations.
2. Implicit Type Conversion (Automatic Type Conversion)
Implicit type conversion, also known as automatic type conversion or type coercion, is a feature of the C programming language that automatically converts one data type to another without requiring explicit type casting by the programmer. It occurs when an operation involves operands of different data types, and the compiler automatically performs the necessary conversion to match the operand types.
The rules for implicit type conversion are defined by the C language standard and are based on a hierarchy of data types, with some types being considered more “prominent” than others. In general, the compiler will automatically promote the operands to a common type based on this hierarchy.
For example, if we have an expression involving an integer and a floating-point number, the compiler will automatically convert the integer to a floating-point number to perform the operation. Consider the following example:
int num = 10;
float result = num / 3;
In this case, the division operation involves an integer (num
) and a floating-point number (3
). The compiler will implicitly convert num
to a floating-point number before performing the division, resulting in a floating-point result.
Implicit type conversion simplifies programming by automatically handling type conversions in certain situations. However, it’s important for programmers to be aware of these conversions and consider their potential impact on the accuracy and precision of calculations in their code.
Automatic type conversion follows the following rules:
- If the data types involved in an operation are different, they are first converted to the same type before the operation takes place.
- Conversion is performed in the direction of increasing data length to ensure that precision is not lost. For example, when an int and a long are involved in an operation, the int value is first converted to long before the operation is performed.
- All floating-point operations are performed using double precision, even if the expression contains only float operands. The float operands are first converted to double before the operation takes place.
- When a char or short is involved in an operation, it must be converted to int first.
- In assignment operations, if the data types on both sides of the assignment operator are different, the data type of the expression on the right side is converted to match the data type of the variable on the left side. If the data type of the expression on the right side is longer than that of the variable on the left side, some data may be lost, resulting in a loss of precision.
Example 2.1) Automatic Type Conversion
#include <stdio.h>
int main()
{
float PI=3.14159;
int s1, r=5;
double s2;
s1 = r*r*PI;
s2 = r*r*PI;
printf("s1=%d, s2=%f\n", s1, s2);
return 0;
}
Results:
s1=78, s2=78.539749
When evaluating the expression r*r*PI
, both r
and PI
are converted to the double
type, and the result of the expression is also of type double
. However, since s1
is of integer type, the result of the assignment operation will still be of integer type. In this case, the decimal part is directly truncated (discarded), rather than rounded up using rounding rules.
It’s important to note that the truncation of the decimal part occurs because the assignment is made to an integer variable, and integers cannot store decimal values.
Whether it is through explicit or implicit type conversion, it is important to note that the purpose is to temporarily convert the data length of a variable for the specific operation at hand. It does not change the data type defined for that variable when it is used elsewhere in the code. The type conversion is temporary and applicable only within the scope of the particular expression or operation where it is performed.
Example 2.2 The Type Conversion is Temporary
#include <stdio.h> int main(){ float f=5.75; printf("(int)f=%d, f=%f\n",(int)f, f); return 0; }
results:
(int)f=5, f=5.750000