Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[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;
 
  • Like
Reactions: baby_1

    baby_1

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

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top