[comp.lang.c] Unsigned considerations

m5@bobkat.UUCP (Mike McNally ) (02/03/88)

Given a compiler with different sized ``int'' and ``long'', what is the
result of:

    int i;
    unsigned long ul;

    ul = i;

Will the assignment be treated as

    ul = (unsigned long) (unsigned) i;

or as

    ul = (unsigned long) (long) i;

How does the ANSI thing say this conversion should be done?

If you think this is completely obvious and that only a fool would get 
confused, please explain why it's obvious.  It's not at all clear to me 
what should be done (K&R of course doesn't say because ``unsigned long''
doesn't exists there; I don't have any other references).

-- 
Mike McNally, mercifully employed at Digital Lynx ---
    Where Plano Road the Mighty Flood of Forest Lane doth meet,
    And Garland fair, whose perfumed air flows soft about my feet...
uucp: {texsun,killer,infotel}!pollux!bobkat!m5 (214) 238-7474

henry@utzoo.uucp (Henry Spencer) (02/07/88)

>     int i;
>     unsigned long ul;
> 
>     ul = i;
> 
> How does the ANSI thing say this conversion should be done?

X3J11 (November draft) says  ul = (unsigned long)(long)i;  is how it happens.
This makes a certain amount of sense:  adjusting the size before adjusting
the signedness produces results that I would say are (a) less surprising,
and (b) more portable across variations in relative sizes of data types.
-- 
Those who do not understand Unix are |  Henry Spencer @ U of Toronto Zoology
condemned to reinvent it, poorly.    | {allegra,ihnp4,decvax,utai}!utzoo!henry

chris@trantor.umd.edu (Chris Torek) (02/08/88)

Speaking of unsigned conversions, unless it has changed recently (which
I doubt), the dpANS says that given

	unsigned short u;

	... <expression involving u> ...

the type of that part of the expression may be either `int' (signed)
or `unsigned int' (unsigned) depending on the relative ranges of
`int' and `unsigned short'.  The expression will be of type `int'
if all unsigned short values (e.g., 0..65535) can be fit into a
signed integer (e.g., on a VAX).  The expression will be `unsigned
int' if an integer cannot hold values in 0..65535 (e.g., on a
PDP-11).

This is *not* what is done by current PCC variants, and seems to
me a horrible misfeature.
In-Real-Life: Chris Torek, Univ of MD Computer Science, +1 301 454 7163
(hiding out on trantor.umd.edu until mimsy is reassembled in its new home)
Domain: chris@mimsy.umd.edu		Path: not easily reachable