Re: Direct computation of integer limits in K&R2?
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Fri, 14 Mar 2008 14:08:48 -0500
Harald van D?k wrote:
CBFalconer wrote:.... snip ...
Harald van D?k wrote:
#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
.
- Follow-Ups:
- Re: Direct computation of integer limits in K&R2?
- From: Keith Thompson
- Re: Direct computation of integer limits in K&R2?
- From: Harald van Dijk
- Re: Direct computation of integer limits in K&R2?
- References:
- Direct computation of integer limits in K&R2?
- From: santosh
- Re: Direct computation of integer limits in K&R2?
- From: Harald van Dijk
- Re: Direct computation of integer limits in K&R2?
- From: CBFalconer
- Re: Direct computation of integer limits in K&R2?
- From: Harald van Dijk
- Direct computation of integer limits in K&R2?
- Prev by Date: Re: Direct computation of integer limits in K&R2?
- Next by Date: Re: Declared global variable isn't being seen by main. - TPA
- Previous by thread: Re: Direct computation of integer limits in K&R2?
- Next by thread: Re: Direct computation of integer limits in K&R2?
- Index(es):
Relevant Pages
|