Re: Direct computation of integer limits in K&R2?



Harald van D?k wrote:
CBFalconer wrote:
Harald van D?k wrote:

.... snip ...

#include <stdio.h>
int main(void) {
unsigned u = -1;
int i;
while ((i = u) < 0 || i != u)
u = u >> 1;
printf("INT_MAX == %u\n", u);
}

This is not guaranteed to work in C99, where the conversion of
an out-of- range integer may raise a signal, but it's valid C90,
since the result of the conversion must be a valid int, and
therefore between INT_MIN and INT_MAX.

This CAN'T work everywhere.

Yes, it can.

The u = -1 statement is legal, and results in UINT_MAX value.
However the first i = u statement always overruns the INT_MAX
value for i, unless the system has INT_MAX defined to be equal
to UINT_MAX. Very rare. So the result of that statement is
implementation defined.

Of course. And any of the permissible implementation-defined
values of i will result in the intended behaviour.

I think you have a misinterpretation of 'implementaion-defined'.
This means that the action, on a particular implementation, is
defined. However that may well be "*** ABORT in line nnn, integer
overflow ***".

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

.



Relevant Pages

  • Re: Problem with linker
    ... but to have actually written a default constructor. ... such as overloading on int and pointer types. ... conversion of 0 to CString requires a user-defined conversion, ... acceleration operator *(distance d, time_squared t2); ...
    (microsoft.public.vc.mfc)
  • Re: Interesting list Validity (True/False)
    ... TypeError: int argument required ... If the gmpy writers are not allowing the conversion, ... it raises an OverflowError and the "%d" formatting ...
    (comp.lang.python)
  • Re: [PATCH 1/2] Add function to convert between calendar time and broken-down time for universal
    ... convert time between calendar time and broken-down time. ... replace `int' with `unsigned int', ... needed of your email client is performing tab-to-space conversion. ... * Library General Public License for more details. ...
    (Linux-Kernel)
  • Re: Re: [PATCH 1/2] Add function to convert between calendar time and broken-down time for unive
    ... A example is my patch for fs/fat/misc.c. ... replace `int' with `unsigned int', ... needed of your email client is performing tab-to-space conversion. ... * Library General Public License for more details. ...
    (Linux-Kernel)
  • Re: if(a);
    ... The type of the octal signed int literal 0 will be compared to the ... 'a' has type unsigned int, in which case 0 is converted to ... is still 0 after undergoing conversion, ...
    (comp.lang.c)