C integer division and floor

CodeKingPlusPlus picture CodeKingPlusPlus · Sep 3, 2012 · Viewed 54.7k times · Source

In C, is there a difference between integer division a/b and floor(a/b) where both a and b are integers? More specifically what happens during both processes?

Answer

Pete Becker picture Pete Becker · Sep 3, 2012

a/b does integer division. If either a or b is negative, the result depends on the compiler (rounding can go toward zero or toward negative infinity in pre-C99; in C99+, the rounding goes toward 0). The result has type int. floor(a/b) does the same division, converts the result to double, discards the (nonexistent) fractional part, and returns the result as a double.