On Wed, 12 Mar 2008 03:29:53 +0530, santosh wrote:
> Harald van D?k wrote:
>> On Wed, 12 Mar 2008 03:07:48 +0530, santosh wrote:
>>> Hello all,
>>>
>>> In K&R2 one exercise asks the reader to compute and print the limits
>>> for the basic integer types. This is trivial for unsigned types. But
>>> is it possible for signed types without invoking undefined behaviour
>>> triggered by overflow? Remember that the constants in limits.h cannot
>>> be used.
>>
>> #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.
>
> Thanks. What about the minima?
Up to INT_MIN, you can use this same idea, except start from LONG_MIN
instead of UINT_MAX. For LONG_MIN, I would cheat with
strtol("-999999999", 0, 0)
adding 9s until a range error is returned.