div

2 つの整数値の商および剰余を計算します。

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

パラメーター

  • numer
    分子。

  • denom
    分母。

戻り値

int 型の引数を使用して div を呼び出すと、商と剰余で構成される div_t 型の構造体が返されます。 long 型の引数を使用するオーバーロードの戻り値は、ldiv_t です。 div_t と ldiv_t は、共に STDLIB.H で定義されます。

解説

div 関数は、numer を denom で割り、商および剰余を求めます。 div_t 構造体は、商 intquot および剰余 intrem で構成されます。 商の符号は、算術上の商の符号と同じです。 商の絶対値は、算術上の商の絶対値より小さい最大の整数です。 分母が 0 の場合、プログラムは終了し、エラー メッセージが表示されます。

long 型の引数を受け取るオーバーロードは、C++ コードのみで使用できます。 ldiv_t 型の戻り値は、longquot と longrem で構成され、各要素は div_t と同じです。

必要条件

ルーチン

必須ヘッダー

div

<stdlib.h>

互換性の詳細については、「C ランタイム ライブラリ」の「互換性」を参照してください。

使用例

// 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 );
}
  

同等の .NET Framework 関数

該当なし標準 C 関数を呼び出すには、PInvoke を使用します。詳細については、「プラットフォーム呼び出しの例」を参照してください。

参照

参照

浮動小数点サポート

ldiv