Because the code is dividing an integer by an integer.
You used a literal number which is an integer value. When you consider only integers, the division of 1 by 3 gives 0 the same. After the calculation results in zero, it is converted to float
by the rule of casting automatic. But note that this casting only occurs with the result as a whole and not in each individual operand.
#include <stdio.h>
int main() {
printf("o valor do numero e': %4.2f", 1.0f/3.0f);
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
Using the numeric literal for the floating point type (1.0f for example), the division occurs in the correct way.
Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?
– Maniero
Always prefer (*) to use
double
instead offloat
. (*) except when the teacher keeps insisting after you explain to him the advantages ofdouble
(is the only descantagem that does not make sense in "programetas").– pmg