Compartilhar via


div, ldiv, lldiv

Calcula o quociente e o resto dos dois valores inteiros.

Sintaxe

div_t div(
   int numer,
   int denom
);
ldiv_t ldiv(
   long numer,
   long denom
);
lldiv_t lldiv(
   long long numer,
   long long denom
);
ldiv_t div(
   long numer,
   long denom
); /* C++ only */
lldiv_t div(
   long long numer,
   long long denom
); /* C++ only */

Parâmetros

numer
O numerador.

denom
O denominador.

Retornar valor

div chamado por meio de argumentos do tipo int retorna uma estrutura do tipo div_t, que contém o quociente e o resto. O valor retornado com argumentos de tipo long é ldiv_t, e o valor retornado com argumentos de tipo long long é lldiv_t. Os tipos div_t, ldiv_t e lldiv_t são definido no <stdlib.h>.

Comentários

A função div divide numer por denom e calcula o quociente e o resto. A estrutura div_t contém o quociente, quot, e o resto, rem. O sinal do quociente é o mesmo que o sinla do quociente matemático. Seu valor absoluto é o maior inteiro menor que o valor absoluto do quociente matemático. Se o denominador for 0, o programa será encerrado com uma mensagem de erro.

As sobrecargas de div que usam argumentos de tipo long ou long long só estão disponíveis para o código do C++. Os tipos de retorno ldiv_t e lldiv_t contém os membros quot e rem, que têm o mesmo significado que os membros do div_t.

Requisitos

Rotina Cabeçalho necessário
div, ldiv, lldiv <stdlib.h>

Para obter informações sobre compatibilidade, consulte Compatibilidade.

Exemplo

// 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

Confira também

Suporte a matemática e ponto flutuante
imaxdiv