By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

"(unsigned)" with long/int

P: n/a
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: <mailto:Th*************@gmail.com>

Aug 7 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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
Aug 7 '07 #2

P: n/a
Kenneth Brody <ke******@spamcop.netwrites:
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) ks***@mib.org <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"
Aug 7 '07 #3

P: n/a
pete <pf*****@mindspring.comwrites:
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) ks***@mib.org <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"
Aug 7 '07 #4

P: n/a
Keith Thompson wrote:
>
pete <pf*****@mindspring.comwrites:
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
Aug 7 '07 #5

P: n/a
pete wrote:
>
Keith Thompson wrote:

pete <pf*****@mindspring.comwrites:
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
Aug 7 '07 #6

P: n/a
Keith Thompson wrote:
>
Kenneth Brody <ke******@spamcop.netwrites:
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: <mailto:Th*************@gmail.com>

Aug 8 '07 #7

P: n/a
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
Aug 8 '07 #8

P: n/a
"Kenneth Brody" <ke******@spamcop.netwrote in message
news:46***************@spamcop.net...
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

Aug 8 '07 #9

P: n/a
On Wed, 08 Aug 2007 11:42:50 -0400, Kenneth Brody
<ke******@spamcop.netwrote:
>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
Aug 8 '07 #10

P: n/a
On Wed, 08 Aug 2007 17:46:10 GMT, Al Balmer <al******@att.netwrote:
On Wed, 08 Aug 2007 11:42:50 -0400, Kenneth Brody
<ke******@spamcop.netwrote:
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.
<OTAnd used as the return type for POSIX read() and write(), and
send() and recv() etc., because they can return EITHER a count (>= 0
<= specified/desired len in call) OR -1 for error.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Aug 26 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.