C programming stdlib.h function - div_t div(int numer, int denom)

The div() function is declared in the stdlib.h header file in C programming language. It takes two integer arguments numer and denom, and returns a structure of type div_t, which contains the quotient and remainder of the division.

The prototype of the div() function is as follows:

refe‮r‬ to:theitroad.com
div_t div(int numer, int denom);

The div() function is used to perform an integer division operation and return both the quotient and remainder of the division. The div_t structure has two fields: quot and rem, which represent the quotient and remainder, respectively.

For example, if we want to divide 10 by 3, the div() function would return a div_t structure with a quotient of 3 and a remainder of 1. The following code snippet shows an example usage of the div() function:

#include <stdlib.h>
#include <stdio.h>

int main() {
    div_t result = div(10, 3);

    printf("10 divided by 3 is %d with a remainder of %d\n", result.quot, result.rem);

    return 0;
}

The output of the above code will be:

10 divided by 3 is 3 with a remainder of 1