div, ldiv, lldiv

두 정수 값의 몫과 나머지를 계산합니다.

구문

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

매개 변수

numer
분자입니다.

denom
분모입니다.

반환 값

div형식 int 의 인수를 사용하여 호출하면 몫과 re기본der가 포함된 형식div_t의 구조체가 반환됩니다. 형식 인수가 있는 반환 값은 형식 long 의 인수 long long 가 있는 반환 값입니다lldiv_t.ldiv_tldiv_tlldiv_t 형식은 div_tstdlib.h>에 <정의됩니다.

설명

함수는 divnumerdenom 과 re기본der를 구분하고 계산합니다. div_t 구조체에는 몫인 quot와 나머지인 rem이 포함됩니다. 몫의 부호는 수학 몫의 부호와 같습니다. 절대값은 수학 몫의 절대값보다 작은 가장 큰 정수입니다. 분모가 0이면 프로그램이 종료되고 오류 메시지가 표시됩니다.

형식 longdiv 인수를 사용하거나 long long C++ 코드에서만 사용할 수 있는 오버로드입니다. 반환 형식 ldiv_tlldiv_t 멤버 quot 를 포함 하 rem고의 멤버 div_t와 동일한 의미를 가진 합니다.

요구 사항

루틴에서 반환된 값 필수 헤더
div, ldiv, lldiv <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 );
}
x is 876, y is 13
The quotient is 67, and the remainder is 5

참고 항목

수학 및 부동 소수점 지원
imaxdiv