On 3/19/2010 1:49 AM, Phred Phungus wrote:

> It's nice to see that russians are free enough to spread spam porn.

>

> I read elsewhere that signed integer division was undefined in C, with

> maybe a change having come in C99.

>

> Is that true?
No, with Yes.

17 / 3 and 17 % 3 are and always have been well-defined,

evaluating to 5 and 2, respectively.

17 / -3 and 17 % -3 are also well-defined, and have been

ever since the ANSI Standard was adopted. But in that Standard

they could evaluate to -5 and -2 or to -6 and 1, the choice

being left to the implementation. The implementation had to

document its choice (and could not produce some third result),

so again the division operation was well-defined, although

(potentially) defined differently on different systems, just

as CHAR_MIN is zero on some systems, negative on others.

C99 *did* make a change: It removed the implementation's

freedom of choice. The results must now be -5 and -2, and the

pair -6 and 1 are forbidden. The operations are still defined.

Integer division or mod by zero, signed or unsigned, has

always been undefined. There's a rumor that the Committee is

considering making INT_MIN % -1 (which must now be 0) either

undefined or implementation-defined, I'm not sure which.

--

Eric Sosman

(E-Mail Removed)lid