"(unsigned)" with long/int

K

Kenneth Brody

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:[email protected]>
 
P

pete

Kenneth said:
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)
 
K

Keith Thompson

Kenneth Brody said:
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.
 
K

Keith Thompson

pete said:
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.
 
P

pete

Keith said:
pete said:
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.
 
P

pete

pete said:
Keith said:
pete said:
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)
 
K

Kenneth Brody

Keith said:
Kenneth Brody said:
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:[email protected]>
 
S

Stephen Sprunk

Kenneth Brody said:
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
 
A

Al Balmer

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.
 
D

David Thompson

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

<OT> And 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top