[SOLVED] Cast Programming problem in C

Status
Not open for further replies.

baby_1

Advanced Member level 1
Joined
Dec 3, 2010
Messages
415
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Activity points
4,277
Hello
here is my program

Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <string.h>
void baby(int x,int y);
int main(void)
{    
int x,y;
scanf("%d%d",&x,&y);
baby(x,y);
scanf("%d",&x);
}
void baby(int x,int y)
{
     float f;
     f=(float)(x/y);
     printf("%f",f);
   
}

if i type 8 and 3 the result is 2 .
if i changed the one of the baby function argument to float it works fine(2.66667).why this code doesn't work properly with cast tranform ( f=(float)(x/y))

i want to do PIC microcontroler programming with C language and i want to decrease my variable bit.

Thanks
 

if i type 8 and 3 the result is 2 .
if i changed the one of the baby function argument to float it works fine(2.66667).why this code doesn't work properly with cast tranform ( f=(float)(x/y))
Because first you divide integers (8/3) and you get 2, which is wrong. Then you cast it to float, but the result is wrong already. You must divide float values from the beginning. Try this and post back the results:

Code:
f=(float)x/(float)y;
 
Reactions: baby_1

    baby_1

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…