What is the difference between signed and unsigned char?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tinesan@gmail.com

    What is the difference between signed and unsigned char?

    Hello fellow C programmers,

    I'm just learning to program with C, and I'm wondering what the
    difference between signed and unsigned char is. To me there seems to
    be no difference, and the standard doesn't even care what a normal char
    is (because signed and unsigned have equal behavior).

    For example if someone does this:

    unsigned char a = -2; /* or = 254 */
    signed char b = -2; /* or = 254 */

    putchar(a);
    putchar(b); /* both print the same character (ex ascii 254)*/

    -------------
    It seems to me that it doesn't matter whether char is signed or
    unsigned, because the output functions just look at the bit pattern and
    deal with it as a positive number.
    Also, I assigned a negative number to unsigned char, it wraps around
    and creates the same bit pattern as assigning the same negative number
    to signed char.

    So my question is, what really is the difference between unsigned and
    signed char?

    Also, for other integral types, are the normal types always equal to
    the signed types (int = signed int, long = signed long,etc. etc.)... or
    is that implementation defined just like for chars?
    Any help will be appreciated.

  • Kobu

    #2
    Re: What is the difference between signed and unsigned char?


    tine...@gmail.c om wrote:[color=blue]
    > Hello fellow C programmers,
    >
    > I'm just learning to program with C, and I'm wondering what the
    > difference between signed and unsigned char is. To me there seems to
    > be no difference, and the standard doesn't even care what a normal[/color]
    char[color=blue]
    > is (because signed and unsigned have equal behavior).
    >
    > For example if someone does this:
    >
    > unsigned char a = -2; /* or = 254 */
    > signed char b = -2; /* or = 254 */[/color]

    I don't think you can assign a negative initializer to a signed
    integer.
    Am I right people?

    Comment

    • Gregory Dean

      #3
      Re: What is the difference between signed and unsigned char?

      The other way around...
      unsigned char does not have a sign extension.


      On 1/27/05 4:21 PM, in article
      1106860918.8553 93.84140@c13g20 00...legro ups.com, "Kobu"
      <kobu.selva@gma il.com> wrote:
      [color=blue]
      >
      > tine...@gmail.c om wrote:[color=green]
      >> Hello fellow C programmers,
      >>
      >> I'm just learning to program with C, and I'm wondering what the
      >> difference between signed and unsigned char is. To me there seems to
      >> be no difference, and the standard doesn't even care what a normal[/color]
      > char[color=green]
      >> is (because signed and unsigned have equal behavior).
      >>
      >> For example if someone does this:
      >>
      >> unsigned char a = -2; /* or = 254 */
      >> signed char b = -2; /* or = 254 */[/color]
      >
      > I don't think you can assign a negative initializer to a signed
      > integer.
      > Am I right people?
      >[/color]

      Comment

      • Alex Fraser

        #4
        Re: What is the difference between signed and unsigned char?

        <tinesan@gmail. com> wrote in message
        news:1106857638 .520980.260880@ f14g2000cwb.goo glegroups.com.. .[color=blue]
        > I'm just learning to program with C, and I'm wondering what the
        > difference between signed and unsigned char is. To me there seems to
        > be no difference, and the standard doesn't even care what a normal char
        > is (because signed and unsigned have equal behavior).
        >
        > For example if someone does this:
        >
        > unsigned char a = -2; /* or = 254 */[/color]

        In this, the value -2, of type int, is converted to unsigned char. This
        conversion is specified as being equivalent to repeatedly adding or
        subtracting UCHAR_MAX + 1 (where UCHAR_MAX is the maximum value an unsigned
        char can have; apparently 255 for your compiler) until the result is between
        0 and UCHAR_MAX inclusive. So a is assigned the value 254.
        [color=blue]
        > signed char b = -2; /* or = 254 */[/color]

        Here, -2, again of type int, is converted to signed char. Note however that
        the effect of assigning 254 to b (which can hold values between SCHAR_MIN
        and SCHAR_MAX inclusive, probably -128 and 127 respectively in your case) is
        undefined by the standards.
        [color=blue]
        > putchar(a);
        > putchar(b); /* both print the same character (ex ascii 254)*/[/color]

        The putchar function takes an int, so for both these calls, the argument is
        converted to type int; the calls are equivalent to putchar(254) and
        putchar(-2) respectively. The putchar function is specified as converting
        its parameter to unsigned char, which uses the rule above. Therefore, with
        UCHAR_MAX being 255, the second call is equivalent to the first in terms of
        the result.
        [color=blue]
        > It seems to me that it doesn't matter whether char is signed or
        > unsigned, because the output functions just look at the bit pattern and
        > deal with it as a positive number.[/color]

        See above.
        [color=blue]
        > Also, I assigned a negative number to unsigned char, it wraps around
        > and creates the same bit pattern as assigning the same negative number
        > to signed char.[/color]

        See the rule above. The wrapping around is what the standards specify. The
        fact it is the same bit pattern is common, because two's complement
        representation for signed numbers is common, but two's complement is not
        required by the standards.
        [color=blue]
        > So my question is, what really is the difference between unsigned and
        > signed char?[/color]

        One can represent unsigned values, and the other signed values (obviously).
        As indicated above, conversion of out-of-range values to signed types (such
        as signed char) is undefined by the standard. Similarly, the result of
        arithmetic that produces out-of-range values for the type is undefined. But
        the behaviour in both these cases for unsigned types (such as unsigned char)
        *is* defined.
        [color=blue]
        > Also, for other integral types, are the normal types always equal to
        > the signed types (int = signed int, long = signed long,etc. etc.)... or
        > is that implementation defined just like for chars?[/color]

        char can represent the same range of values as either signed char or
        unsigned char, but all three are distinct types. (Similarly, int and long
        may be able to represent the same range of values on a given implementation,
        but they too are distinct types.)

        int, short, long (and long long in C99) are always capable of representing
        negative numbers. I think that int and signed int are the same type, and
        similarly for short, long and long long. Hopefully someone else can clarify
        this point.

        HTH,
        Alex


        Comment

        • Peter Nilsson

          #5
          Re: What is the difference between signed and unsigned char?

          tinesan@gmail.c om wrote:[color=blue]
          >
          > For example if someone does this:
          >
          > unsigned char a = -2; ...
          > signed char b = -2; ...
          >
          > putchar(a);
          > putchar(b); /* both print the same character (ex ascii 254)*/[/color]

          Abstractly, putchar() is a wrapper for fputc(), and...

          "The fputc function writes the character specified by c
          (converted to an unsigned char)..."

          So, the calls will both send the same byte value to stdout.
          [color=blue]
          > -------------
          > It seems to me that it doesn't matter whether char is signed
          > or unsigned, because the output functions just look at the bit
          > pattern and deal with it as a positive number.[/color]

          No, the conversion is _NOT_ specified in terms of bit pattern.

          On a signed magnitude machine, the 8-bit representation of -2 is...

          10000010

          Irrespective of the signed char representation, the conversion
          will always yield UCHAR_MAX + 1 - 2.
          [color=blue]
          > Also, I assigned a negative number to unsigned char, it wraps
          > around and creates the same bit pattern as assigning the same
          > negative number to signed char.[/color]

          It will do so on two's complement machines. However, the standard
          doesn't _require_ two's complement integer representation for
          negative signed integers.
          [color=blue]
          > So my question is, what really is the difference between unsigned
          > and signed char?
          >
          > Also, for other integral types, are the normal types always equal
          > to the signed types (int = signed int, long = signed long,etc.
          > etc.)...[/color]

          Yes.
          [color=blue]
          > or is that implementation defined just like for chars?[/color]

          No.

          --
          Peter

          Comment

          • Peter Nilsson

            #6
            Re: What is the difference between signed and unsigned char?

            Gregory Dean wrote:[color=blue]
            > The other way around...
            > unsigned char does not have a sign extension.[/color]

            Please don't top post in clc.
            [color=blue]
            > kobu.selva@gmai l.com> wrote:[color=green][color=darkred]
            > > >
            > > > I'm just learning to program with C, and I'm wondering what
            > > > the difference between signed and unsigned char is.[/color][/color][/color]

            Apart from the obvious!?
            [color=blue][color=green][color=darkred]
            > > > To me there seems to be no difference, and the standard
            > > > doesn't even care what a normal char is (because signed and
            > > > unsigned have equal behavior).[/color][/color][/color]

            You haven't read the standard, have you?

            The value of an unsigned integer cannot be negative.
            [color=blue][color=green][color=darkred]
            > > > For example if someone does this:
            > > >
            > > > unsigned char a = -2; /* or = 254 */[/color][/color][/color]

            /* or UCHAR_MAX + 1 - 2 */
            [color=blue][color=green][color=darkred]
            > > > signed char b = -2; /* or = 254 */[/color][/color][/color]

            Since -2 is in the range -127..127, the minimum range for
            signed char, b will always be assigned the value -2 here
            on any conforming implementation.
            [color=blue][color=green]
            > > I don't think you can assign a negative initializer to a signed
            > > integer.
            > >
            > > Am I right people?[/color][/color]

            No.

            --
            Peter

            Comment

            • Keith Thompson

              #7
              Re: What is the difference between signed and unsigned char?

              "Kobu" <kobu.selva@gma il.com> writes:[color=blue]
              > tine...@gmail.c om wrote:[color=green]
              >> Hello fellow C programmers,
              >>
              >> I'm just learning to program with C, and I'm wondering what the
              >> difference between signed and unsigned char is. To me there seems to
              >> be no difference, and the standard doesn't even care what a normal[/color]
              > char[color=green]
              >> is (because signed and unsigned have equal behavior).
              >>
              >> For example if someone does this:
              >>
              >> unsigned char a = -2; /* or = 254 */
              >> signed char b = -2; /* or = 254 */[/color]
              >
              > I don't think you can assign a negative initializer to a signed
              > integer.
              > Am I right people?[/color]

              No. Both declarations above are legal.

              --
              Keith Thompson (The_Other_Keit h) kst-u@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.

              Comment

              • Chris Croughton

                #8
                Re: What is the difference between signed and unsigned char?

                On 27 Jan 2005 12:27:18 -0800, tinesan@gmail.c om
                <tinesan@gmail. com> wrote:
                [color=blue]
                > Hello fellow C programmers,
                >
                > I'm just learning to program with C, and I'm wondering what the
                > difference between signed and unsigned char is. To me there seems to
                > be no difference, and the standard doesn't even care what a normal char
                > is (because signed and unsigned have equal behavior).
                >
                > For example if someone does this:
                >
                > unsigned char a = -2; /* or = 254 */
                > signed char b = -2; /* or = 254 */
                >
                > putchar(a);
                > putchar(b); /* both print the same character (ex ascii 254)*/[/color]

                On your machine. They won't on a 1-s complement machine. Incidentally,
                value 254 (0xFE) is not an ASCII character, ASCII only defines 7 bit
                characters in the range 0x00 to 0x7FF (0 to 127).
                [color=blue]
                > -------------
                > It seems to me that it doesn't matter whether char is signed or
                > unsigned, because the output functions just look at the bit pattern and
                > deal with it as a positive number.[/color]

                Usually, yes. Not guaranteed, though (some output libraries will fail
                if the parameter is negative).
                [color=blue]
                > Also, I assigned a negative number to unsigned char, it wraps around
                > and creates the same bit pattern as assigning the same negative number
                > to signed char.[/color]

                Again, on your machine.
                [color=blue]
                > So my question is, what really is the difference between unsigned and
                > signed char?[/color]

                int main(void)
                {
                unsigned char a = -2;
                signed char b = -2;

                if (a == b)
                printf("equal\n ");

                if (a > 0)
                printf("a > 0\n");

                if (b > 0)
                printf("b > 0\n");
                return 0;
                }

                What, if anything, will be output?
                [color=blue]
                > Also, for other integral types, are the normal types always equal to
                > the signed types (int = signed int, long = signed long,etc. etc.)... or
                > is that implementation defined just like for chars?[/color]

                They are defined to take the same amount of storage. It is also defined
                that a signed value which is positive can be converted to an unsigned
                value of the same type and back again with no change in value. However,
                an unsigned value which is greater than the maximum positive value which
                a signed version can hold cannot be reliably converted into a signed
                value, nor is it defined what value an unsigned version of a negative
                value has.

                Chris C

                Comment

                • CBFalconer

                  #9
                  Re: What is the difference between signed and unsigned char?

                  Chris Croughton wrote:[color=blue]
                  >[/color]
                  .... snip on signed/unsigned char ...[color=blue]
                  >
                  > They are defined to take the same amount of storage. It is also defined
                  > that a signed value which is positive can be converted to an unsigned
                  > value of the same type and back again with no change in value. However,
                  > an unsigned value which is greater than the maximum positive value which
                  > a signed version can hold cannot be reliably converted into a signed
                  > value, nor is it defined what value an unsigned version of a negative
                  > value has.[/color]

                  That last provision is wrong. There is a specific process for
                  converting any out-of-range value to an unsigned value. It just
                  isn't necessarily reversible.

                  --
                  "If you want to post a followup via groups.google.c om, don't use
                  the broken "Reply" link at the bottom of the article. Click on
                  "show options" at the top of the article, then click on the
                  "Reply" at the bottom of the article headers." - Keith Thompson


                  Comment

                  • Gregory Dean

                    #10
                    Re: What is the difference between signed and unsigned char?

                    On 1/27/05 6:01 PM, in article
                    1106866915.1646 01.291800@f14g2 00...legr oups.com, "Peter Nilsson"
                    <airia@acay.com .au> wrote:
                    [color=blue]
                    > Gregory Dean wrote:[color=green]
                    >> The other way around...
                    >> unsigned char does not have a sign extension.[/color]
                    > lly pre
                    > Please don't top post in clc.
                    >[color=green]
                    >> kobu.selva@gmai l.com> wrote:[color=darkred]
                    >>>>
                    >>>> I'm just learning to program with C, and I'm wondering what
                    >>>> the difference between signed and unsigned char is.[/color][/color]
                    >
                    > Apart from the obvious!?
                    >[color=green][color=darkred]
                    >>>> To me there seems to be no difference, and the standard
                    >>>> doesn't even care what a normal char is (because signed and
                    >>>> unsigned have equal behavior).[/color][/color]
                    >
                    > You haven't read the standard, have you?
                    >
                    > The value of an unsigned integer cannot be negative.
                    >[color=green][color=darkred]
                    >>>> For example if someone does this:
                    >>>>
                    >>>> unsigned char a = -2; /* or = 254 */[/color][/color]
                    >
                    > /* or UCHAR_MAX + 1 - 2 */
                    >[color=green][color=darkred]
                    >>>> signed char b = -2; /* or = 254 */[/color][/color]
                    >
                    > Since -2 is in the range -127..127, the minimum range for
                    > signed char, b will always be assigned the value -2 here
                    > on any conforming implementation.
                    >[color=green][color=darkred]
                    >>> I don't think you can assign a negative initializer to a signed
                    >>> integer.
                    >>>
                    >>> Am I right people?[/color][/color]
                    >
                    > No.[/color]

                    Silly preferences setting in Entourage 2004. Sorry.
                    -Greg

                    Comment

                    • pete

                      #11
                      Re: What is the difference between signed and unsigned char?

                      Chris Croughton wrote:[color=blue]
                      >
                      > On 27 Jan 2005 12:27:18 -0800, tinesan@gmail.c om
                      > <tinesan@gmail. com> wrote:
                      >[color=green]
                      > > Hello fellow C programmers,
                      > >
                      > > I'm just learning to program with C, and I'm wondering what the
                      > > difference between signed and unsigned char is.
                      > > To me there seems to
                      > > be no difference, and the standard doesn't even
                      > > care what a normal char
                      > > is (because signed and unsigned have equal behavior).
                      > >
                      > > For example if someone does this:
                      > >
                      > > unsigned char a = -2; /* or = 254 */
                      > > signed char b = -2; /* or = 254 */
                      > >
                      > > putchar(a);
                      > > putchar(b); /* both print the same character (ex ascii 254)*/[/color]
                      >
                      > On your machine. They won't on a 1-s complement machine.[/color]

                      They won't do what exactly, on a 1-s complement machine?

                      ((unsigned char)-2) may or may not be 254,
                      but putchar(-2) does the same thing
                      as putchar((unsign ed char)-2), always.

                      See Alex Fraser's post in this thread for a good explanation.

                      --
                      pete

                      Comment

                      Working...