Next: Floating Point Numbers, Previous: Integers, Up: Arithmetic
This section describes functions for performing integer division. These
functions are redundant when GNU CC is used, because in GNU C the
`/' operator always rounds towards zero. But in other C
implementations, `/' may round differently with negative arguments.
div and ldiv are useful because they specify how to round
the quotient: towards zero. The remainder has the same sign as the
numerator.
These functions are specified to return a result r such that the value
r.quot*denominator + r.rem equals
numerator.
To use these facilities, you should include the header file stdlib.h in your program.
This is a structure type used to hold the result returned by the
divfunction. It has the following members:
int quot- The quotient from the division.
int rem- The remainder from the division.
This function
divcomputes the quotient and remainder from the division of numerator by denominator, returning the result in a structure of typediv_t.If the result cannot be represented (as in a division by zero), the behavior is undefined.
Here is an example, albeit not a very useful one.
div_t result; result = div (20, -6);Now
result.quotis-3andresult.remis2.
This is a structure type used to hold the result returned by the
ldivfunction. It has the following members:
long int quot- The quotient from the division.
long int rem- The remainder from the division.
(This is identical to
div_texcept that the components are of typelong intrather thanint.)
The
ldivfunction is similar todiv, except that the arguments are of typelong intand the result is returned as a structure of typeldiv_t.
This is a structure type used to hold the result returned by the
lldivfunction. It has the following members:
long long int quot- The quotient from the division.
long long int rem- The remainder from the division.
(This is identical to
div_texcept that the components are of typelong long intrather thanint.)
The
lldivfunction is like thedivfunction, but the arguments are of typelong long intand the result is returned as a structure of typelldiv_t.The
lldivfunction was added in ISO C99.
This is a structure type used to hold the result returned by the
imaxdivfunction. It has the following members:
intmax_t quot- The quotient from the division.
intmax_t rem- The remainder from the division.
(This is identical to
div_texcept that the components are of typeintmax_trather thanint.)See Integers for a description of the
intmax_ttype.
The
imaxdivfunction is like thedivfunction, but the arguments are of typeintmax_tand the result is returned as a structure of typeimaxdiv_t.See Integers for a description of the
intmax_ttype.The
imaxdivfunction was added in ISO C99.