signed char and unsigned char difference

Discussion in 'C Programming' started by dam_fool_2003@yahoo.com, Jul 25, 2004.

  1. Guest

    For int data type the default range starts from signed to unsigned. If
    we don't want negative value we can force an unsigned value. The same
    goes for long also.
    But I don't understand why we have signed char which is -256. Does it
    means that we can assign the same ASCII value to both signed and
    unsigned. That means the ASCII value can be represented with a type of
    signed char and also unsigned char?
    For example
    int main(void)
    {
    signed char a= 'a';
    unsigned char b = 'b';
    printf("%i %c",a,b);
    return 0;
    }

    The above code does not warn about the assignment. I went through the
    faq ,
    Section 8 but I don't find the answer. Can any one give any pointer
    regarding
    the above subject?
     
    , Jul 25, 2004
    #1
    1. Advertising

  2. -berlin.de Guest

    wrote:
    > For int data type the default range starts from signed to unsigned. If
    > we don't want negative value we can force an unsigned value. The same
    > goes for long also.


    Sorry, but these sentences don't make sense to me. For signed ints
    the data range is INT_MIN to INT_MAX, for unsigned ints it's 0 to
    UINT_MAX. INT_MIN must be at least -32767, INT_MAX +32767 and
    UINT_MAX 65535. For long there are similar minimum ranges, with
    "INT" replaced by "LONG" (i.e. LONG_MAX instead of INT_MAX) with
    minimum requirements being LONG_MIN == -2^31-1, LONG_MAX == 2^31-1
    and ULONG_MAX == 2^32-1. Implementations are allowed to support
    larger ranges. The actual values can be found in <limits.h>.

    > But I don't understand why we have signed char which is -256.


    The range for signed chars is SCHAR_MIN to SCHAR_MAX. Quite often
    (on machines with 8 bits in a char and 2's complement) this is the
    range between -128 and +127. The range for unsigned char is 0 to
    UCHAR_MAX (quite often this is 0 to 255). The ranges of -127 to
    127 for signed and 0 to 255 for unsigned chars are the minimum
    requirements, so you can be sure you can store numbers from these
    ranges wherever you have a standard compliant C compiler. While
    there probably are some machines where you also could store -256
    in a signed char you shouldn't rely on this, on many machines it
    won't work.

    > Does it
    > means that we can assign the same ASCII value to both signed and
    > unsigned. That means the ASCII value can be represented with a type of
    > signed char and also unsigned char?


    Yes, since ASCII characters are all in the range between 0 and 127,
    thus they can always be stored in a signed as well as an unsigned
    char.

    > For example
    > int main(void)
    > {
    > signed char a= 'a';
    > unsigned char b = 'b';


    There's nothing the compiler should complain about as long as you're
    using ASCII (it's different with EBCDIC because there 'a' is 129 and
    also most of the other letters are above 127, so you would better use
    unsigned char).
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Jul 25, 2004
    #2
    1. Advertising

  3. Tim Prince Guest

    <-berlin.de> wrote in message
    news:...
    > wrote:


    > > Does it
    > > means that we can assign the same ASCII value to both signed and
    > > unsigned. That means the ASCII value can be represented with a type of
    > > signed char and also unsigned char?

    >
    > Yes, since ASCII characters are all in the range between 0 and 127,
    > thus they can always be stored in a signed as well as an unsigned
    > char.
    >
    > > For example
    > > int main(void)
    > > {
    > > signed char a= 'a';
    > > unsigned char b = 'b';

    >
    > There's nothing the compiler should complain about as long as you're
    > using ASCII (it's different with EBCDIC because there 'a' is 129 and
    > also most of the other letters are above 127, so you would better use
    > unsigned char).

    It's been a while, but I once used a PRIMOS system where the default ASCII
    representation had the high bit set.
     
    Tim Prince, Jul 25, 2004
    #3
  4. On Sun, 25 Jul 2004 wrote:

    > For int data type the default range starts from signed to unsigned. If
    > we don't want negative value we can force an unsigned value. The same
    > goes for long also.


    First sentence doesn't make sense to me. The rest seems obviously true.

    > But I don't understand why we have signed char which is -256. Does it
    > means that we can assign the same ASCII value to both signed and
    > unsigned. That means the ASCII value can be represented with a type of
    > signed char and also unsigned char?


    The first sentence of this paragraph makes no sense to me. There are
    systems where a char is 16 bits. For these systems you can have a signed
    char with a value of -256. Maybe the confusion is that char is not used to
    hold characters. It can be used for that purpose but it can also be used
    as an integer data type with a very small range of values. If you need to
    save space and you never need anything outside the range of a char, then
    use a char.

    As to your question, the ASCII character set is in the range 0 to 127. A
    signed char is typically in the range -128 to 127. An unsigned char is
    typically in the range 0 to 255. The ASCII character set will fit in both
    ranges. If you are on a system where CHAR_BIT (see <limits.h>) is greater
    than 8 the range could be even larger, so it would still hold true.

    > For example
    > int main(void)
    > {
    > signed char a= 'a';
    > unsigned char b = 'b';
    > printf("%i %c",a,b);
    > return 0;
    > }


    If you give printf a %i it is expecting an int. You are passing it a
    signed char. This will have undefind behaviour. Did you mean to use:

    printf("%c %c\n", a, b);

    > The above code does not warn about the assignment. I went through the
    > faq , Section 8 but I don't find the answer. Can any one give any
    > pointer regarding the above subject?


    --
    Send e-mail to: darrell at cs dot toronto dot edu
    Don't send e-mail to
     
    Darrell Grainger, Jul 25, 2004
    #4
  5. -berlin.de writes:
    [...]
    > There's nothing the compiler should complain about as long as you're
    > using ASCII (it's different with EBCDIC because there 'a' is 129 and
    > also most of the other letters are above 127, so you would better use
    > unsigned char).


    On any system that uses EBCDIC as the default encoding, plain char
    will almost certainly be unsigned.

    To oversimplify slightly:

    Use plain char to hold characters (the implementation will have chosen
    an appropriate representation). Use unsigned char to hold bytes. Use
    signed char to hold very small numeric values. (I actually haven't
    seen much use for explicitly signed char.)

    --
    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.
     
    Keith Thompson, Jul 25, 2004
    #5
  6. Jack Klein Guest

    On Sun, 25 Jul 2004 16:06:37 GMT, "Tim Prince"
    <> wrote in comp.lang.c:

    >
    > <-berlin.de> wrote in message
    > news:...
    > > wrote:

    >
    > > > Does it
    > > > means that we can assign the same ASCII value to both signed and
    > > > unsigned. That means the ASCII value can be represented with a type of
    > > > signed char and also unsigned char?

    > >
    > > Yes, since ASCII characters are all in the range between 0 and 127,
    > > thus they can always be stored in a signed as well as an unsigned
    > > char.
    > >
    > > > For example
    > > > int main(void)
    > > > {
    > > > signed char a= 'a';
    > > > unsigned char b = 'b';

    > >
    > > There's nothing the compiler should complain about as long as you're
    > > using ASCII (it's different with EBCDIC because there 'a' is 129 and
    > > also most of the other letters are above 127, so you would better use
    > > unsigned char).

    > It's been a while, but I once used a PRIMOS system where the default ASCII
    > representation had the high bit set.


    Then it wasn't ASCII.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jul 25, 2004
    #6
  7. Jack Klein Guest

    On 25 Jul 2004 19:31:17 GMT,
    (Darrell Grainger) wrote in comp.lang.c:

    > On Sun, 25 Jul 2004 wrote:
    >
    > > For int data type the default range starts from signed to unsigned. If
    > > we don't want negative value we can force an unsigned value. The same
    > > goes for long also.

    >
    > First sentence doesn't make sense to me. The rest seems obviously true.
    >
    > > But I don't understand why we have signed char which is -256. Does it
    > > means that we can assign the same ASCII value to both signed and
    > > unsigned. That means the ASCII value can be represented with a type of
    > > signed char and also unsigned char?

    >
    > The first sentence of this paragraph makes no sense to me. There are
    > systems where a char is 16 bits. For these systems you can have a signed
    > char with a value of -256. Maybe the confusion is that char is not used to
    > hold characters. It can be used for that purpose but it can also be used
    > as an integer data type with a very small range of values. If you need to
    > save space and you never need anything outside the range of a char, then
    > use a char.
    >
    > As to your question, the ASCII character set is in the range 0 to 127. A
    > signed char is typically in the range -128 to 127. An unsigned char is
    > typically in the range 0 to 255. The ASCII character set will fit in both
    > ranges. If you are on a system where CHAR_BIT (see <limits.h>) is greater
    > than 8 the range could be even larger, so it would still hold true.
    >
    > > For example
    > > int main(void)
    > > {
    > > signed char a= 'a';
    > > unsigned char b = 'b';
    > > printf("%i %c",a,b);
    > > return 0;
    > > }

    >
    > If you give printf a %i it is expecting an int. You are passing it a
    > signed char. This will have undefind behaviour. Did you mean to use:


    No, he is not. One can't pass any type of char as argument to a
    variadic function beyond the specified ones. The char 'a' will be
    promoted to int and the behavior is perfectly defined.

    Technically, passing unsigned char 'b' to printf() with a "%c"
    conversion specifier could be undefined because:

    1. The implementation might have UCHAR_MAX > INT_MAX (in other words,
    UCHAR_MAX == UINT_MAX) and so 'b' will be converted to unsigned,
    rather than signed, int.

    2. The standard suggests, but does not require, that the signed and
    unsigned integer types be interchangeable as function argument and
    return types.

    So this just could be undefined on a platform where the character
    types have the same number of bits as int (there are some, believe me)
    and unsigned ints are passed to variadic functions differently than
    signed ints are.

    I would not hold my breath waiting for such an implementation to
    appear. From a QOI point of view it would be horrible.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jul 25, 2004
    #7
  8. writes:
    > For int data type the default range starts from signed to unsigned. If
    > we don't want negative value we can force an unsigned value. The same
    > goes for long also.


    True.

    > But I don't understand why we have signed char which is -256.


    We don't.

    The smallest value that fits in 8-bits (which is the minimal size a
    signed char can hold, IIRC) is not -256 but -128. But your programs
    shouldn't depend on that. Use SCHAR_MIN instead of an inline "magic"
    value and you'll be fine ;-)

    > Does it means that we can assign the same ASCII value to both signed
    > and unsigned.


    An unsigned char can hold values up to UCHAR_MAX. I'm not sure if
    converting this value to signed char and back to unsigned will always
    work as expected.

    > That means the ASCII value can be represented with a type of signed
    > char and also unsigned char?


    No. SCHAR_MAX is usually 127 (if char values have 8-bits), which is
    smaller than some of the values that unsigned characters can store.

    > For example
    >
    > int main(void)
    > {
    > signed char a= 'a';
    > unsigned char b = 'b';
    > printf("%i %c",a,b);
    > return 0;
    > }
    >
    > The above code does not warn about the assignment.


    It depends on the warnings you have enabled. Here it doesn't even build
    because printf() is called before a prototype is visible:

    foo.c:5: warning: implicit declaration of function `printf'

    Giorgos
     
    Giorgos Keramidas, Jul 26, 2004
    #8
  9. Old Wolf Guest

    -berlin.de wrote:

    > > For example
    > > int main(void)
    > > {
    > > signed char a= 'a';
    > > unsigned char b = 'b';

    >
    > There's nothing the compiler should complain about as long as you're
    > using ASCII (it's different with EBCDIC because there 'a' is 129 and
    > also most of the other letters are above 127, so you would better use
    > unsigned char).


    The type 'char' has to be able to represent all members of the basic
    character set, which includes 'a'. If the machine had 8-bit chars and
    'a' == 129, then it must have 'char' being unsigned.
     
    Old Wolf, Jul 26, 2004
    #9
  10. "Jack Klein" <> wrote...
    > (Darrell Grainger) wrote in comp.lang.c:
    > > On Sun, 25 Jul 2004 wrote:
    > > > For example
    > > > int main(void)
    > > > {
    > > > signed char a= 'a';


    Unless plain char is signed, there's no requirement that the value of 'a'
    fit within the range of signed char.

    > > > unsigned char b = 'b';
    > > > printf("%i %c",a,b);
    > > > return 0;
    > > > }

    > >
    > > If you give printf a %i it is expecting an int. You are passing it a
    > > signed char. This will have undefind behaviour. Did you mean to use:

    >
    > No, he is not. One can't pass any type of char as argument to a
    > variadic function beyond the specified ones. The char 'a' will be
    > promoted to int


    This is ambiguous since a literal 'a' is already of int and no promotion
    would be required. Of course, Jack is talking about the promotion of signed
    and unsigned chars a and b respectively when used as arguments to printf.

    > and the behavior is perfectly defined.


    --
    Peter
     
    Peter Nilsson, Jul 26, 2004
    #10
    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. Replies:
    10
    Views:
    1,102
  2. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    605
    Jack Klein
    May 9, 2005
  3. At_sea_with_C

    char vs. signed or unsigned char

    At_sea_with_C, Mar 14, 2007, in forum: C Programming
    Replies:
    4
    Views:
    760
    Malcolm McLean
    Mar 14, 2007
  4. Ioannis Vranos
    Replies:
    11
    Views:
    773
    Ioannis Vranos
    Mar 28, 2008
  5. Ioannis Vranos

    Padding bits and char, unsigned char, signed char

    Ioannis Vranos, Mar 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    624
    Ben Bacarisse
    Mar 29, 2008
Loading...

Share This Page