type casting

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
Eradat
New poster
Posts: 3
Joined: Fri Nov 05, 2004 8:35 pm

type casting

Post by Eradat »

I want to know about type casting,if u know tell me some.
thanks
:roll:
shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA

Post by shamim »

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]
Eradat
New poster
Posts: 3
Joined: Fri Nov 05, 2004 8:35 pm

I want more ...

Post by Eradat »

I want more . When we must use "(type)" (type=int or float or ...)
in our code?
Thanks
:roll: :roll:
Eradat
New poster
Posts: 3
Joined: Fri Nov 05, 2004 8:35 pm

Post by Eradat »

I'm still waiting :roll:
Mohammad Mahmudur Rahman
Experienced poster
Posts: 154
Joined: Sat Apr 17, 2004 9:34 am
Location: EEE, BUET

Post by Mohammad Mahmudur Rahman »

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.
Post Reply

Return to “C++”