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?
ANSI C merely refers to a particular standard for the C Programming Language - i.e. there is no difference, they refer to the same thing.
ANSI C, ISO C, and Standard C are successive standards for the C programming language published by the American National Standards Institute (ANSI) and ISO/IEC JTC 1/SC 22/WG 14 of the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC).
The ANSI C standard provides a predefined symbol named __STDC__ that is set to 1 when the compiler is enforcing strict ANSI standard conformance. If you want your programs to be 100 percent ANSI conformant, you should ensure that the __STDC__ symbol is defined.
Let's continue with a discussion of all the five different standards of C — K&R C, ANSI C, C99, C11 and Embedded C. For the purposes of our discussion, the compiler used is the gcc C compiler from the GNU Compiler Collection (GCC).
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 quotienta/b
is representable, the expression(a/b)*b + a%b
shall equala
.
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 quotienta/b
is representable, the expression(a/b)*b + a%b
shall equala
.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.
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::
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With