Type conversation and casting
Type conversation and
type casting :-
(i). Type Conversion. :-
The type conversion is the process of converting a data value from one data type to another data
type automatically by the compiler. Sometimes type conversion is also called implicit type
conversion. The implicit type conversion is automatically performed by the compiler.
For example, in c programming language, when we assign an integer value to a float variable the
integer value automatically gets converted to float value by adding decimal value 0. And when a
float value is assigned to an integer variable the float value automatically gets converted to an
integer value by removing the decimal value. To understand more about type conversion observe
the following...
int i = 10 ;
float x = 15.5 ;
char ch = 'A' ;
i = x ; =======> x value 15.5 is converted as 15 and assigned to variable i
x = i ; =======> Here i value 10 is converted as 10.000000 and assigned to variable x
i = ch ; =======> Here the ASCII value of A (65) is assigned to i
Example:π
#include<stdio.h>
#include<conio.h>
void main(){
int i = 95 ;
float x = 90.99 ;
char ch = 'A' ;
i = x ;
printf("i value is %d\n",i);
x = i ;
printf("x value is %f\n",x);
i = ch ;
printf("i value is %d\n",i);
}
Output:π i value is 90.
x value is 90.000000
i value is 65.
In the above program, we assign i = x, i.e., float variable value is assigned to the integer variable.
Here, the compiler automatically converts the float value (90.99) into integer value (90) by
removing the decimal part of the float value (90.99) and then it is assigned to variable i. Similarly,
when we assign x = i, the integer value (90) gets converted to float value (90.000000) by adding
zero as the decimal part.
(ii). Typecasting:- π
Typecasting is also called an explicit type conversion. Compiler converts data from one data type to another data type implicitly. When compiler converts implicitly, there may be a data loss.
In such a case, we convert the data from one data type to another data type using explicit type
conversion. To perform this we use the unary cast operator. To convert data from one type to
another type we specify the target data type in parenthesis as a prefix to the data value that has to
be converted. The general syntax of typecasting is as follows: (Target Data type) Data Value
Example :-π
int total Marks = 450, max Marks = 600 ;
float average ;
average = (float) total Marks / max Marks * 100 ;
In the above example code, both total Marks and max Marks are integer data values. When we
perform total Marks / max Marks the result is a int value, but the destination (average) data type is
a float. So we use type casting to convert Total Marks and max Marks into float data type.
Example Program:-
#include<stdio.h>
#include<conio.h>
int main()
{
int a, b, c;
float avg;
printf("Enter any three integer values");
scanf("%d%d%d",&a,&b,&c);
printf("a=%d, b=%d, c=%d",a,b,c);
avg = (a+b+c)/3 ;
printf("\n avg before casting = %f",avg);
avg = (float)(a + b + c) / 3 ;
printf("\n avg before casting = %f",avg);
return 0;
}
Output:-
Enter any three integer values 5 6 8
a=5, b=6, c=8
avg before casting = 6.000000
avg before casting = 6.333333
Comments
Post a Comment