Search code examples
cintegerdivisioninteger-divisionfloor

C integer division and floor


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?


Solution

  • 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.