I want to divide two integers and result in 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));
}
But the code below, which I believe to be equivalent to the one 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 is this happening?