20

I seem to remember that ANSI C didn't specify what value should be returned when either operand of a modulo operator is negative (just that it should be consistent). Did it get specified later, or was it always specified and I am remembering incorrectly?

Philip Potter
  • 8,975
  • 2
  • 37
  • 47
Chas. Owens
  • 64,182
  • 22
  • 135
  • 226

2 Answers2

40

C89, not totally (§3.3.5/6). It can be either -5 or 5, because -5 / 10 can return 0 or -1 (% is defined in terms of a linear equation involving /, * and +):

When integers are divided and the division is inexact, if both operands are positive the result of the / operator is the largest integer less than the algebraic quotient and the result of the % operator is positive. If either operand is negative, whether the result of the / operator is the largest integer less than the algebraic quotient or the smallest integer greater than the algebraic quotient is implementation-defined, as is the sign of the result of the % operator. If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a.

C99, yes (§6.5.5/6), the result must be -5:

When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded.88) If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a.

88) This is often called "truncation toward zero".


Similarly, in C++98 the result is implementation defined (§5.6/4), following C89's definition, but mentions that the round-towards-zero rule is preferred,

... If both operands are nonnegative then the remainder is nonnegative; if not, the sign of the remainder is implementation-defined74).

74) According to work underway toward the revision of ISO C, the preferred algorithm for integer division follows the rules defined in the ISO Fortran standard, ISO/IEC 1539:1991, in which the quotient is always rounded toward zero.

and indeed it becomes the standard rule in C++0x (§5.6/4):

... For integral operands the / operator yields the algebraic quotient with any fractional part discarded;82 ...

82) This is often called truncation towards zero.

Community
  • 1
  • 1
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
3

To add a little detail to KennyTM's answer: If the C Standards call something implementation defined then that implementation is required to document the choice it makes. Usually this would be in the compiler or library documentation (man page, help manual, printed docs, CD booklet :-) Any implementation claiming conformance to C89 or later must provide this somewhere. Try looking for such a document. In the case of gcc for example, this is in the gcc-info:

4 C Implementation-defined behavior


A conforming implementation of ISO C is required to document its choice of behavior in each of the areas that are designated "implementation defined". The following lists all such areas, along with the section numbers from the ISO/IEC 9899:1990 and ISO/IEC 9899:1999 standards. Some areas are only implementation-defined in one version of the standard.

Some choices depend on the externally determined ABI for the platform (including standard character encodings) which GCC follows; these are listed as "determined by ABI" below. *Note Binary Compatibility: Compatibility, and `http://gcc.gnu.org/readings.html'. Some choices are documented in the preprocessor manual. *Note Implementation-defined behavior: (cpp)Implementation-defined behavior. Some choices are made by the library and operating system (or other environment when compiling for a freestanding environment); refer to their documentation for details.

  • Menu:

  • Translation implementation::

  • Environment implementation::
  • Identifiers implementation::
  • Characters implementation::
  • Integers implementation::
  • Floating point implementation::
  • Arrays and pointers implementation::
  • Hints implementation::
  • Structures unions enumerations and bit-fields implementation::
  • Qualifiers implementation::
  • Declarators implementation::
  • Statements implementation::
  • Preprocessing directives implementation::
  • Library functions implementation::
  • Architecture implementation::
  • Locale-specific behavior implementation::
Jens
  • 69,818
  • 15
  • 125
  • 179