2
I wish to carry out the division of two integers and have as a result a floating point value.
The following code works normally:
#include <stdio.h>
float divInts(int x, int y)
{
return((float)x/y);
}
void main()
{
printf("%f",divInts(50,3));
}
However, the code below, which I believe is equivalent to the above, does not work as expected, returning the value 1.031615 when dividing 50/3, instead of 16.666667:
#include <stdio.h>
void divInts()
{
int x;
int y;
float resultado;
printf ("Entre o numerador 'x' :\n");
scanf ("%f", &x);
printf ("Entre o denominador 'y' :\n");
scanf ("%f", &y);
resultado = (float)x / y;
printf("O resultado da divisao entre x e y, em formato ponto flutuante e : %f\n\n", resultado);
}
void main()
{
divInts();
}
Why does this happen?