Floor division in C and "/" operator

image

Here is the expression table 4.8.1 from the book; it suggests that when using the “/” operator with integers that they will be automatically typecast into floats. Is this true?
I was wondering whether the “/” operator also is overloaded as a floor division with integers (the counterpart to remainder from mod). If it’s not, is there another expression or built in function to access that?
Desired output: 8 / 3 = 2 for ints

1 Like

Since f could be anything from \{ int, long, float, double \}, if f was int, the division will also provide an int.

Therefore the following program

# include <stdio.h>

int main() {
    printf("%d\n", (8/3));
    return 0;
}

will actually print 2.

If you would change one of the numbers in the division, the Implicit Type Conversions will occur and convert the next higher type (as shown by the info graphic in chapter 4.8.1).

E. g. Change the (integer) 8 to the (double) 8.0:

# include <stdio.h>

int main() {
    printf("%f\n", (8.0/3));
    return 0;
}

which will print 2.666667 - a double.

8 Likes