"(unsigned)" with long/int

Discussion in 'C Programming' started by Kenneth Brody, Aug 7, 2007.

  1. 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:>
    Kenneth Brody, Aug 7, 2007
    #1
    1. Advertising

  2. Kenneth Brody

    pete Guest

    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
    pete, Aug 7, 2007
    #2
    1. Advertising

  3. Kenneth Brody <> 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.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"
    Keith Thompson, Aug 7, 2007
    #3
  4. pete <> 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) <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"
    Keith Thompson, Aug 7, 2007
    #4
  5. Kenneth Brody

    pete Guest

    Keith Thompson wrote:
    >
    > pete <> 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
    pete, Aug 7, 2007
    #5
  6. Kenneth Brody

    pete Guest

    pete wrote:
    >
    > Keith Thompson wrote:
    > >
    > > pete <> 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
    pete, Aug 7, 2007
    #6
  7. Keith Thompson wrote:
    >
    > Kenneth Brody <> 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: <mailto:>
    Kenneth Brody, Aug 8, 2007
    #7
  8. 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
    Richard Heathfield, Aug 8, 2007
    #8
  9. "Kenneth Brody" <> wrote in message
    news:...
    > 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
    Stephen Sprunk, Aug 8, 2007
    #9
  10. Kenneth Brody

    Al Balmer Guest

    On Wed, 08 Aug 2007 11:42:50 -0400, Kenneth Brody
    <> 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
    Al Balmer, Aug 8, 2007
    #10
  11. On Wed, 08 Aug 2007 17:46:10 GMT, Al Balmer <> wrote:

    > On Wed, 08 Aug 2007 11:42:50 -0400, Kenneth Brody
    > <> 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.


    <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
    David Thompson, Aug 26, 2007
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    658
    Eric Sosman
    Jul 8, 2003
  2. sridhar

    comparing unsigned long and unsigned int

    sridhar, Nov 1, 2004, in forum: C Programming
    Replies:
    6
    Views:
    430
    J. J. Farrell
    Nov 3, 2004
  3. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,175
    Peter Shaggy Haywood
    Sep 20, 2005
  4. pereges

    Promoting unsigned long int to long int

    pereges, Jun 30, 2008, in forum: C Programming
    Replies:
    112
    Views:
    2,028
    David Thompson
    Jul 28, 2008
  5. pozz
    Replies:
    12
    Views:
    721
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page