Type casting is used in the conversion between data types. This is usually done when a higher bit data type is converted to that of lower bit.
e.g, to convert from double to int, you would use:
[cpp]
int m;
m = (int)pow(2,10) % 30;
[/cpp]
Casting is particularly useful while working with variables of different variables. Consider the following example which demonstrates the use of casting to get the desired result in a double variable from the division of 2 integers.
[c]
#include<stdio.h>
int main(void)
{
int x,y,iout;
double dout,dcout;
/* Initializations */
x = 5;
y = 2;
/* Division of 2 integers stored in another integer */
iout = x / y;
printf("%d\n",iout);
/* Division of 2 integers stored in a double */
dout = x / y;
printf("%lf\n",dout);
/*As no casting is done the fraction of x/y is lost
before storing it into the double dout */
/* Division of 2 integers stored in a double with casting*/
dcout = (double)x / y;
printf("%lf\n",dcout);
/* Fractional part preserved */
return 0;
}
[/c]
You should never take more than you give in the circle of life.