Sometimes, we need to convert from one type to another type as per the requirement. This conversion technique is known as type casting.
There are two types of casting: Implicit and Explicit. Implicit type casting is performed, automatically without the programmer's intervention, whereas explicit conversion is performed by the programmer mentioning desired data type.
Memory allocation for a variable is dependent on data type, hence converting data from one type to another may result in loss of data.
In this context, if we run the following code, for values of m = 10 and n = 3 the result will be 3. But actually, the result should be >3.3333...
#include <stdio.h>
int main(void)
{
int m, n;
float f;
printf("\nEnter two integer:");
scanf("%d %d", &m, &n);
f = m / n;
printf("The result is %f\n", f);
return 0;
}
The output of the above program is shown below.
Hence, to overcome this problem, we need to convert m as a floating point number before diving it by n (known as explicit type casting). Hence, the updated code is rewritten below:
#include <stdio.h>
int main(void)
{
int m, n;
float f;
printf("\nEnter two integer:");
scanf("%d %d", &m, &n);
f = (float)m / n;
printf("The result is %f\n", f);
return 0;
}
The output of the above program is shown below. You might have noticed that the result is obtained as per our expectations.
More Examples of Type Casting
In the following program, we have shown some examples. Here, x is a floating point number, which is converted to an integer using i = (int) x;. It truncates the digits after the decimal places as the fractional part can not be stored in an integer variable, though floating point numbers and integer numbers require the same size of memory (4 bytes) on my system. In some systems, floating point numbers and integers may have different sizes.
Using ch = (char) x;, we are trying to allocate 4 bytes of data in 1-byte space. Hence, the data is truncated. If we print the value of ch variable, it prints a garbage value.
In case of conversion of integer to floating point number, the data is not lost. However, in the case of integer-to-character conversion, the data is truncated as shown in the next section of the program.
In the last section, when we are assigning character variables to integers and floating point numbers, the data is not lost, though the output is 42 and 42.000000. The reason behind this is a small data is being allocated to a relatively large space. In the print statement, instead of using %d and %f, you may try %c.
#include <stdio.h>
int main()
{
float x;
int i;
char ch;
x = 2.345;
i = (int) x;
ch = (char) x;
printf("From float x =%f i =%d ch =%c\n", x, i, ch);
i = 45;
x = (float) i;
ch = (char) i;
printf("From int i=%d x=%f ch=%c\n", i, x, ch);
ch = '*';
i = (int) ch;
x = (float) ch;
printf("From char ch=%c i=%d x=%f\n", ch, i, x);
return 0;
}
In the first and second examples, we have discussed why explicit type casting is required. The following example shows that sometimes type casting is performed automatically. Here, we have taken the previous example code and removed the explicit type casting. The output remains unchanged.
#include <stdio.h>
int main()
{
float x;
int i;
char ch;
x = 2.345;
i = x;
ch = x;
printf("From float x =%f i =%d ch =%c\n", x, i, ch);
i = 45;
x = i;
ch = i;
printf("From int i=%d x=%f ch=%c\n", i, x, ch);
ch = '*';
i = ch;
x = ch;
printf("From char ch=%c i=%d x=%f\n", ch, i, x);
return 0;
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.