Answer the question
In order to leave comments, you need to log in
Magic int when dividing by float?
Hello. C began to study quite recently, and here, as it turned out, there is a lot of "magic" that I do not understand.
#include <stdio.h>
int main()
{
int a = 13;
float b = 4.7;
printf("%d", b/a); //на выходе получаем 660764199
printf("%d", b*a); //а здесь - -1207959552
return 0;
}
Answer the question
In order to leave comments, you need to log in
Weird numbers are taken when one type is stored in a memory cell, and printf expects to see another, therefore it incorrectly represents it. Printf will not round or otherwise convert types for you, what they tell him, he will output.
and then in printf output through the conversion character for integers, i.e. %d
printf("%d", (int)(b*a));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question