Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer


Computes the quotient and the remainder of two integer values.

div_t div( 
   int numer,
      int denom 
ldiv_t div(
      long numer,
   long denom



The numerator.


The denominator.

div called with arguments of type int returns a structure of type div_t, comprising the quotient and the remainder. The return value of the overload with arguments of type long is ldiv_t. Both div_t and ldiv_t are defined in STDLIB.H.

The div function divides numer by denom, computing the quotient and the remainder. The div_t structure contains int quot, the quotient, and int rem, the remainder. The sign of the quotient is the same as that of the mathematical quotient. Its absolute value is the largest integer that is less than the absolute value of the mathematical quotient. If the denominator is 0, the program terminates with an error message.

The overload taking arguments of type long is only available to C++ code. The return type ldiv_t contains the members long quot and long rem, which have the same meanings as the members of div_t.

Routine Required header Compatibility



ANSI, Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003

For additional compatibility information, see Compatibility in the Introduction.

// crt_div.c
// arguments: 876 13

// This example takes two integers as command-line
// arguments and displays the results of the integer
// division. This program accepts two arguments on the
// command line following the program name, then calls
// div to divide the first argument by the second.
// Finally, it prints the structure members quot and rem.

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

int main( int argc, char *argv[] )
   int x,y;
   div_t div_result;

   x = atoi( argv[1] );
   y = atoi( argv[2] );

   printf( "x is %d, y is %d\n", x, y );
   div_result = div( x, y );
   printf( "The quotient is %d, and the remainder is %d\n",
           div_result.quot, div_result.rem );


x is 876, y is 13
The quotient is 67, and the remainder is 5

Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.

© 2016 Microsoft