Подтвердить что ты не робот

Как я могу получить частное и остальное за один шаг?

Возможный дубликат:
Разделить и получить остаток в то же время?

Можно ли получить как частное, так и остальное целочисленное деление за один шаг, т.е. без выполнения целочисленного деления дважды?

4b9b3361

Ответ 1

div сделает это. См. reference и пример:

/* div example */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  div_t divresult;
  divresult = div (38,5);
  printf ("38 div 5 => %d, remainder %d.\n", divresult.quot, divresult.rem);
  return 0;
}

Вывод:

38 div 5 => 7, remainder 3.

EDIT:

В спецификации C указано:

7.20 Общие утилиты

The types declared are size_t and wchar_t (both described in 7.17),
div_t
which is a structure type that is the type of the value returned by the div function,
ldiv_t
which is a structure type that is the type of the value returned by the ldiv function, and
lldiv_t
which is a structure type that is the type of the value returned by the lldiv function.

... но он не говорит, что такое определение div_t.

Ответ 2

Да, существует стандартная функция div()ldiv, а может быть даже lldiv), которая делает это.