C Programming/stdlib.h/div

From Wikibooks, open books for an open world
Jump to: navigation, search

div is a function in C programming language that takes two integers as parameters and returns the result of a division between them. It is specified in ANSI-C, and is included from the stdlib.h header when used. [1]

div always rounds towards 0, unlike ordinary integer division in C, where rounding for negative numbers is implementation-dependent.

div has a prototype as follows:

div_t div (int numerator, int denominator)

The return value, div_t is a special datatype which is specifically used in storing the results of this function. It is defined as follows:

typedef struct {
  int quot;
  int rem;
} div_t;

Where quot stores the quotient and rem stores the remainder.

ldiv and lldiv are similar functions that divide integers of type long and long long, respectively; and return a structure of type ldiv_t and lldiv_t, respectively.

ldiv_t ldiv (long numerator, long denominator );
lldiv_t lldiv (long long numerator, long long denominator);

See also[edit]

  • stdlib.h
  • stdio.h


External links[edit]