Newsgroups: comp.lang.c Path: utzoo!henry From: henry@utzoo.uucp (Henry Spencer) Subject: Re: checking for overflow in C Message-ID: <1989May6.224226.22085@utzoo.uucp> Organization: U of Toronto Zoology References: <13367@dartvax.Dartmouth.EDU> Date: Sat, 6 May 89 22:42:26 GMT In article <13367@dartvax.Dartmouth.EDU> shallit@eleazar.dartmouth.edu (Jeffrey Shallit) writes: >Fans of C frequently boast that it is "very close to the machine". Since >I'm a relative newcomer to the language, perhaps someone could enlighten >me about the officially approved way of checking overflow when multiplying >two integers... C unfortunately has to be close to the hardware of a wide variety of machines. There is no consensus on whether the hardware checks for overflow or not. C generally prefers to reflect the underlying hardware rather than trying to hide it, so this lack of consensus shows to the programmer. C does not promise that overflow will be detected at all. Nor does C promise that overflow *won't* be detected; it might abort your program, or just do strange things. For reliable and portable code, you must do the check yourself, in advance (and be careful that your overflow check can't itself cause an overflow!). -- Mars in 1980s: USSR, 2 tries, | Henry Spencer at U of Toronto Zoology 2 failures; USA, 0 tries. | uunet!attcan!utzoo!henry henry@zoo.toronto.edu