tech-net archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: foo = (unsigned)bar; (was Re: dhcpcd-4.0.0-alpha1 ready for review)



On Apr 18,  9:33am, roy%marples.name@localhost (Roy Marples) wrote:
-- Subject: foo = (unsigned)bar; (was Re: dhcpcd-4.0.0-alpha1 ready for revie

| On Thursday 17 April 2008 21:47:25 David Laight wrote:
| > On Thu, Apr 17, 2008 at 11:00:28AM +0100, Roy Marples wrote:
| > > I've also changed all instances of my ctype calls I could find to cast to
| > > unsigned.
| >
| > You do mean 'unsigned char' don't you ?
| 
| OK, this piqued my curiosity last night. What would this be expected to do
| 
| signed char bar;
| unsigned char foo;
| foo = (unsigned)bar;
| 
| IMO, only two things are possible here
| 1) type is preserved, so bar is correctly cast to an unsigned char
| 2) type is silently changed, so bar is incorrectly cast to an unsigned (and I 
| guess) int
| 
| I can't find any documentation on this, so does anyone have any opinion that 
| can hopefully be backed up with reference documentation?

This is not the interesting case, because the end result is the same (i.e
if bar = -2; foo ends up being 254). But if foo was unsigned int, it would
end up being 0xfffffffe, since the bar gets promoted to int (and thus
sign-extended) before being casted to unsigned int.

christos


Home | Main Index | Thread Index | Old Index