Re: Questions, please
From: Malcolm (malcolm_at_55bank.freeserve.co.uk)
Date: 02/04/04
- Next message: Sidney Cadot: "Re: C program works on W98 but not new WXP machine"
- Previous message: Ed Morton: "Re: OT: FSM (Finite State Machine) Generator - Open Source"
- In reply to: Eric Sosman: "Re: Questions, please"
- Next in thread: Martin Dickopp: "Re: Questions, please"
- Reply: Martin Dickopp: "Re: Questions, please"
- Reply: Eric Sosman: "Re: Questions, please"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 3 Feb 2004 23:16:56 -0000
"Eric Sosman" <Eric.Sosman@sun.com> wrote in message
> > Actually your example illustrates my point. x = (unsigned int) -1
> > should ideally generate an error.
>
> Why?
>
Because it is nonsense to try to express a negative number as an unsigned
integer.
>
> More probably, because the conversion from signed to
> unsigned integers is well-defined by the Standard, and is
> not an error of any kind.
>
But the standard itself is a human construct. I would imagine that the main
reason it allows it is to avoid breaking hundreds of pre-standard programs.
The reason K and R allowed it was probably efficiency.
>
> The compiler may complain about
> the construct (it may complain about your Argyle socks if
> it so chooses), but it may not reject the program if it's
> free of other errors and exceeds no implementation limits.
> There's no error in what you've shown, assuming `x' has a
> reasonable definition (e.g., not `const FILE* x').
>
Obviously since I say the construct "should ideally generate an error" there
must be circumstances, non-ideal ones, in which the error is not generated.
>
> No: It produces the bit pattern of all ones because that
> is the representation of UINT_MAX on all C-faring machines.
>
So why does the standard specify that -1, of all things, must cast to
UINT_MAX?
>
> No: Even on a ones' complement or signed magnitude machine,
> the result is the same. Of course, on these machines the cast
> does more than the "reinterpretation of the bits" you mentioned
> in an earlier post. WHY does the same value come out? Because
> the conversion from signed int to unsigned int is defined in
> terms of the *values* being converted, not of the bit patterns
> that represent those values.
>
You need to look at the motivation. Read what I said to Jack Klein about
natural language.
-1 casts to 0xFFFF because that is a reinterpretation of bits on two's
complement machines, and because the conversion can be accomplished in a
single machine instruction. Natural language needs to be socially
appropriate as well as literally accurate. In this case I am explaining C
casting to someone who doesn't understand very much about it, so mention of
one's complement machines, or the text of the standard, is not useful and
confuses.
- Next message: Sidney Cadot: "Re: C program works on W98 but not new WXP machine"
- Previous message: Ed Morton: "Re: OT: FSM (Finite State Machine) Generator - Open Source"
- In reply to: Eric Sosman: "Re: Questions, please"
- Next in thread: Martin Dickopp: "Re: Questions, please"
- Reply: Martin Dickopp: "Re: Questions, please"
- Reply: Eric Sosman: "Re: Questions, please"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]