div

計算商和兩個整數值的其餘部分。

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

參數

  • numer
    分子。

  • denom
    分母。

傳回值

div使用型別引數呼叫int傳回型別的結構div_t,可包含所產生的商數,且餘數。 傳回值的多載型別的引數與long是ldiv_t。 兩者都div_t和ldiv_t STDLIB 中所定義。H.

備註

div運作除以numer的denom,計算所產生的商數,且餘數。 The div_t structure contains intquot, the quotient, and intrem, the remainder. 正負號的商數為數學的商數相同。 其數值的絕對值是商之的小於數學絕對值的最大整數。 如果分母為 0,則程式會結束並出現錯誤訊息。

取得型別的引數的多載long才可以使用 C++ 程式碼。 傳回的型別 ldiv_t 包含成員longquot和longrem,其中有相同的意義,為成員的div_t。

需求

常式

所需的標頭

div

<stdlib.h>

其他的相容性資訊,請參閱相容性在簡介中。

範例

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