Why does division result in zero instead of a decimal?

前端 未结 5 703
隐瞒了意图╮
隐瞒了意图╮ 2020-11-21 05:38

Teaching myself C and finding that when I do an equation for a temp conversion it won\'t work unless I change the fraction to a decimal. ie,

tempC=(.555*(tempF

5条回答
  •  萌比男神i
    2020-11-21 06:13

    If you put 5/9 in parenthesis, this will be calculated first, and since those are two integers, it will be done by integer division and the result will be 0, before the rest of the expression is evaluated.

    You can rearrange your expression so that the conversion to float occurs first:

    tempC=((5/9)*(tempF-32));tempC=(5*(tempF-32))/9;

    or of course, as the others say, use floating point constants.

提交回复
热议问题