М. Тэмүүлэн М. Тэмүүлэн - 1 year ago 97
C Question

How to get float by integer / integer C lang

I wanna get 1.666667 by dividing 2*n+1 by 3 (n=2). and it always creates 1.000000 but i want 1.666667. I tried printing (2*n+1)/3 as a float but it doesn't work.

Answer Source

You are doing integer division and then assigning to a float.



The 2.0 is a double and the whole expression is then converted to a double, which is assigned to a float giving you the result intended.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download