div、ldiv、lldivdiv, ldiv, lldiv

2 つの整数値の商と剰余を計算します。Computes the quotient and the remainder of two integer values.


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


分子。The numerator.

デ omdenom
分母。The denominator.

戻り値Return Value

int型の引数を使用して呼び出されたdivは、商と剰余で構成されるdiv_t型の構造体を返します。div called by using arguments of type int returns a structure of type div_t, which comprises the quotient and the remainder. Long型の引数を持つ戻り値はldiv_tであり、 long long型の引数を持つ戻り値はlldiv_tです。The return value with arguments of type long is ldiv_t, and the return value with arguments of type long long is lldiv_t. div_tldiv_t、およびlldiv_tは、stdlib.h> <> で定義されています。div_t, ldiv_t, and lldiv_t are defined in <stdlib.h>.


Div関数は、数値デ omによって除算し、商と剰余を計算します。The div function divides numer by denom and thereby computes the quotient and the remainder. Div_t構造体には、商、 quot、剰余、 remが含まれています。商の符号は、数学的な商の符号と同じです。The div_t structure contains the quotient, quot, and the remainder, rem. 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. 分母が 0 の場合、プログラムはエラー メッセージにより終了します。If the denominator is 0, the program terminates with an error message.

Long 型または long long型の引数を受け取るC++ divのオーバーロードは、コードでのみ使用できます。The overloads of div that take arguments of type long or long long are only available to C++ code. 戻り値の型 ldiv_tlldiv_tには、 div_tのメンバーと同じ意味を持つメンバー quotremが含まれています。The return types ldiv_t and lldiv_t contains members quot and rem, which have the same meanings as the members of div_t.


ルーチンによって返される値Routine 必須ヘッダーRequired header
divldivlldivdiv, ldiv, lldiv <stdlib.h><stdlib.h>

互換性の詳細については、「 互換性」を参照してください。For additional compatibility information, see Compatibility.


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

関連項目See also

浮動小数点サポートFloating-Point Support
ldiv、lldivldiv, lldiv