How to divide 2 int in c?

后端 未结 5 2078
清酒与你
清酒与你 2020-12-10 18:29

wanna divide 2 numbers and get the result like this:

5 / 2 = 2.50

But it only outputs 2.

I don\'t now what i\'m doing wrong.

Here my code:

5条回答
  •  粉色の甜心
    2020-12-10 18:37

    You have to use float or double variables, not int (integer) ones. Also note that a division between two integers will lead to an integer result, meanwhile a division between a float/double and an integer will lead to a float result. That's because C implicitly promote this integer to float.

    For example:

    5/2 = 2
    5/2.0f = 2.5f
    

    Note the .0f, this actually means that we are dividing with a float.

提交回复
热议问题