What is an "unsigned char" ?

This is a discussion on What is an "unsigned char" ? within the C Programming forums, part of the General Programming Boards category; Reading through some code I have inherited, I found that some variables had been declared as "unsigned char". I understand ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    5

    What is an "unsigned char" ?

    Reading through some code I have inherited, I found that some variables had been declared as "unsigned char".

    I understand what signed/unsigned means in terms of an int data type, but what is the reason behind declaring a char as unsigned?

    The only thing I can think of is that it offers some memory advantage. (This code is used in an embedded system).

    Any ideas?

    Thanks,

    Stephen.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    unsigned char is just a regular char - except that it can't be negative.

    There are 3 declarations
    char ch;
    signed char ch;
    unsigned char ch;

    For various historical reasons, the sign of char was never explictly defined, so its really up to the implementation to say whether a char is either signed or unsigned.

    So my first guess would be the programmer is merely being specific.

    Additionally, the bit-wise operator >> is much better defined for unsigned types than it is for signed types.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    5
    Thanks for the reply Salem, but I still dont fully understand.

    How does allowing the char to be negative (using signed char) affect the code/change the way the char is processed?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Try it

    unsigned char a = 0x80;
    signed char b = 0x80;
    int c = a;
    int d = b;

    Now print c and d
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    5
    So the only difference is that the dec representation of the char takes on a sign.

    (The signed/unsigned part of the variable is ignored when printing out as a char.)

    I still dont fully understand this, but I guess that will only come when I need to use it.

    Thanks again.

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    53
    It all has to do with memory. When you have a char usually you are using 1 byte. A variable as you know has a data type and a name. The data type "unsigned char" has a range from 0 to 255 whereas normal char has a range from -128 to 127. Just like when you use the datatype int you cannot go above 32767 or below -32767 when storing a number as type int. I hope that helps you understand it better.

    If you have any questions e-mail HernGT22@aol.com
    Last edited by hern; 01-15-2004 at 07:12 PM.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>whereas normal char has a range from -128 to 128.
    Not true, try again.

    >>If you have any questions e-mail ...
    I'd suggest people respond to the forum, that way mistakes like the one you just made won't get overlooked
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    The range of integers isn't always what you stated either.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    53
    Unless you use signed or unsigned i believe what I said is true about int, both from what I have read and from my own experience. Also, char has a range from -128 to 127, I can scan the pages of where I found this information. If it isn't true, I think SAMS has a big problem in their books on programming.

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Unless you use signed or unsigned i believe what I said is true about int
    32767 is the max signed integer in the old as dirt 16 bit compilers. To steal a phrase from Salem, any compiler "from this millennium" uses 32 bits putting the max signed integer value at 2147483647.

    Also you said -128 to 128 which is what Hammer was calling you on.

    Third on a 16 bit system it is -32768 to 32767.

    To echo the others, please reply in the threads so that there are people who can monitor and call bull when it appears.

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    Originally posted by Thantos
    32767 is the max signed integer in the old as dirt 16 bit compilers.
    Pet peeve:
    Getting a little tired of people dissing older compilers, implying they don't work well. Get off it. Old compilers DO work well and work just fine up thru XP. I have no problem with suggestions to upgrade -- it makes sense to. But stop bad-mouthing options people have that in fact do work


    Climbing off soapbox, covering head to protect from the coming flames...

    We now return control to your forum.

  12. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    27
    Hi,

    Try the code snippet below in a short program.


    unsigned char a;
    char b;

    a = 0xff;
    b = 0xff;
    printf("\n%d %d\n",a,b);


    I hope that it will answer your question.
    Pappy
    You learn something new everyday.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-07-2006, 11:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21