Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > "(unsigned)" with long/int

Reply
Thread Tools

"(unsigned)" with long/int

 
 
Kenneth Brody
Guest
Posts: n/a
 
      08-07-2007
Does the cast "(unsigned)" imply "(unsigned int)", or does it simply
strip the signedness from the variable?

In other words, given this:

long l = -123;
unsigned long ul = (unsigned)l;

Does the value of "l" get demoted to an unsigned int during the
assignment, or does it remain a long? (I am on a system where
sizeof int == sizeof long, so examining the generated code doesn't
help me out here.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <(E-Mail Removed)>

 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      08-07-2007
Kenneth Brody wrote:
>
> Does the cast "(unsigned)" imply "(unsigned int)",


Yes.

> or does it simply
> strip the signedness from the variable?


No.

> In other words, given this:
>
> long l = -123;
> unsigned long ul = (unsigned)l;
>
> Does the value of "l" get demoted to an unsigned int during the
> assignment or does it remain a long?
> (I am on a system where
> sizeof int == sizeof long, so examining the generated code doesn't
> help me out here.)


The type and value of the assignment expression is:
((unsigned long)(unsigned)-123)

--
pete
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      08-07-2007
Kenneth Brody <(E-Mail Removed)> writes:
> Does the cast "(unsigned)" imply "(unsigned int)", or does it simply
> strip the signedness from the variable?

[...]

In a cast or in any other context, 'unsigned' by itself is an
abbreviation for 'unsigned int'. Similarly, 'long', 'signed long',
'long int', and 'signed long int' are all equivalent.

See section 6.7.2 of the C99 standard (latest draft available at
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf>, or the
corresponding section in any other version or draft of the C standard,
or any decent C textbook.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-07-2007
pete <(E-Mail Removed)> writes:
> Kenneth Brody wrote:

[...]
>> In other words, given this:
>>
>> long l = -123;
>> unsigned long ul = (unsigned)l;
>>
>> Does the value of "l" get demoted to an unsigned int during the
>> assignment or does it remain a long?
>> (I am on a system where
>> sizeof int == sizeof long, so examining the generated code doesn't
>> help me out here.)

>
> The type and value of the assignment expression is:
> ((unsigned long)(unsigned)-123)


Actually, the expression '-123' is of type int; the operand of the
cast to 'unsigned' is 'l', which is of type long.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      08-07-2007
Keith Thompson wrote:
>
> pete <(E-Mail Removed)> writes:
> > Kenneth Brody wrote:

> [...]
> >> In other words, given this:
> >>
> >> long l = -123;
> >> unsigned long ul = (unsigned)l;
> >>
> >> Does the value of "l" get demoted to an unsigned int during the
> >> assignment or does it remain a long?
> >> (I am on a system where
> >> sizeof int == sizeof long, so examining the generated code doesn't
> >> help me out here.)

> >
> > The type and value of the assignment expression is:
> > ((unsigned long)(unsigned)-123)

>
> Actually, the expression '-123' is of type int; the operand of the
> cast to 'unsigned' is 'l', which is of type long.


Thank you.

--
pete
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      08-07-2007
pete wrote:
>
> Keith Thompson wrote:
> >
> > pete <(E-Mail Removed)> writes:
> > > Kenneth Brody wrote:

> > [...]
> > >> In other words, given this:
> > >>
> > >> long l = -123;
> > >> unsigned long ul = (unsigned)l;
> > >>
> > >> Does the value of "l" get demoted to an unsigned int during the
> > >> assignment or does it remain a long?
> > >> (I am on a system where
> > >> sizeof int == sizeof long, so examining the generated code doesn't
> > >> help me out here.)
> > >
> > > The type and value of the assignment expression is:
> > > ((unsigned long)(unsigned)-123)

> >
> > Actually, the expression '-123' is of type int; the operand of the
> > cast to 'unsigned' is 'l', which is of type long.

>
> Thank you.


However, the type and value of this expression:
((unsigned long)(unsigned)-123)
is the same as the type and value of:
((unsigned long)(unsigned)(long)-123)

--
pete
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      08-08-2007
Keith Thompson wrote:
>
> Kenneth Brody <(E-Mail Removed)> writes:
> > Does the cast "(unsigned)" imply "(unsigned int)", or does it simply
> > strip the signedness from the variable?

> [...]
>
> In a cast or in any other context, 'unsigned' by itself is an
> abbreviation for 'unsigned int'. Similarly, 'long', 'signed long',
> 'long int', and 'signed long int' are all equivalent.

[...]

Thanks. That's what I was afraid of.

Drifting OT a little...

I'm just trying to figure out a way around a weirdness in a POSIX-like
library function. (Specifically, read() and write().) Not all
implementations are identical. (Some use int, some use size_t, for
example.)

In this case, the implementation takes an "unsigned int" for the
length, but returns "int" for the length actually read/written.
This means you can't use this without getting a warning:

if ( write(fd,buffer,len) < len )

Not a biggie, as there are ways around this.

Side question: is "ssize_t" (the signed counterpart to "size_t") a
standard type? I see no reference to it in n1124.pdf, so I assume
it's not. (Though it may be standard POSIX.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <(E-Mail Removed)>

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      08-08-2007
Kenneth Brody said:

<snip>

> Side question: is "ssize_t" (the signed counterpart to "size_t") a
> standard type?


No.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      08-08-2007
"Kenneth Brody" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Side question: is "ssize_t" (the signed counterpart to "size_t") a
> standard type? I see no reference to it in n1124.pdf, so I assume
> it's not. (Though it may be standard POSIX.)


POSIX defines ssize_t, but ISO doesn't. It's functionally equivalent to
ptrdiff_t, though most people wouldn't think to use the latter for the same
purposes due to the name.

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Al Balmer
Guest
Posts: n/a
 
      08-08-2007
On Wed, 08 Aug 2007 11:42:50 -0400, Kenneth Brody
<(E-Mail Removed)> wrote:

>Side question: is "ssize_t" (the signed counterpart to "size_t") a
>standard type? I see no reference to it in n1124.pdf, so I assume
>it's not. (Though it may be standard POSIX.)


Standard (and required) POSIX type, not standard C.

--
Al Balmer
Sun City, AZ
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments