div() — Calculate quotient and remainder

Standards

Standards / Extensions C or C++ Dependencies

ISO C
XPG4
XPG4.2
ISO/ANSI C++
C99
Single UNIX Specification, Version 3
C++ TR1 C99

both  

Format

#include <stdlib.h>

div_t div(int numerator, int denominator);
div_t div(long numerator, long denominator);   /* C++ only */
C++ TR1 C99:
#define _TR1_C99
#include <inttypes.h>
imaxdiv_t div(intmax_t numerator, intmax_t denominator);  

#define _TR1_C99 
#include <stdlib.h>
lldiv_t div(long long numerator, long long denominator);

General description

Calculates the quotient and remainder of the division of numerator by denominator.

Special behavior for C++: For C++ applications, div() is also overloaded for the type long.

Returned value

Returns a structure of type div_t, containing both the quotient int quot and the remainder int rem. This structure is defined in stdlib.h. If the returned value cannot be represented, the behavior of div() is undefined. If denominator is 0, the same exception will be raised as if you divided by 0. That is, you get the error CEE3209S (Fixed-point divide exception).