"(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. Advertisements

  2. Kenneth Brody

    pete Guest

    No.
    The type and value of the assignment expression is:
    ((unsigned long)(unsigned)-123)
     
    pete, Aug 7, 2007
    #2
    1. Advertisements

  3. [...]

    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, Aug 7, 2007
    #3
  4. Actually, the expression '-123' is of type int; the operand of the
    cast to 'unsigned' is 'l', which is of type long.
     
    Keith Thompson, Aug 7, 2007
    #4
  5. Kenneth Brody

    pete Guest

    Thank you.
     
    pete, Aug 7, 2007
    #5
  6. Kenneth Brody

    pete Guest

    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, 2007
    #6
  7. [...]

    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:

    No.
     
    Richard Heathfield, Aug 8, 2007
    #8
  9. 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, Aug 8, 2007
    #9
  10. Kenneth Brody

    Al Balmer Guest

    Standard (and required) POSIX type, not standard C.
     
    Al Balmer, Aug 8, 2007
    #10
  11. <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. Advertisements

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 (here). After that, you can post your question and our members will help you out.