Thread: What's the difference between signed and unsigned char and what is the default?

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    217

    What's the difference between signed and unsigned char and what is the default?

    Consider the following:
    Code:
        signed char sign = -140;
        unsigned char sign = -140;
        char sign = -140;
    
        std::cout << sign;
    (I'm using Visual C++ compiler) From what I have learnt, signed char must have values ranging for -127 to +127 and unsigned char from 0 to 255. And the default is signed char.. apparently??

    But in the above code, all of them give me the same output. So what's the difference!? There has gotta be a reason why signed is the only identifier that is applicable to char data type.. I mean it's gotta serve a purpose right..

    I even tried this for loop:
    Code:
        for (int i = 1; (char)1 != (char)i+1; i++) {
            cout << i << " " << (signed char)i << " ;";
        }
    I changed "signed" to "unsigned" but seems both unsigned and signed char is practically THE SAME..?


    Thanks for reading!

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    There is a option to some compiler that set the default sign/unsign of char type.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Sep 2018
    Posts
    217
    But what is the difference...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    The difference:
    • signed char is signed, i.e., in two's complement with an 8-bit byte, you would expect a range from -128 to 127, hence the minimum required range is -127 to 127 as that would be the range for one's complement and sign & magnitude (but note that a "byte" can have more than 8 bits in C, so larger ranges are possible)
    • unsigned char is unsigned, i.e., with an 8-bit byte, you would expect a range from 0 to 255
    • char can be either equivalent to signed char or unsigned char, depending on the compiler
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2018
    Posts
    217
    Quote Originally Posted by laserlight View Post
    The difference:
    • signed char is signed, i.e., in two's complement with an 8-bit byte, you would expect a range from -128 to 127, hence the minimum required range is -127 to 127 as that would be the range for one's complement and sign & magnitude (but note that a "byte" can have more than 8 bits in C, so larger ranges are possible)
    • unsigned char is unsigned, i.e., with an 8-bit byte, you would expect a range from 0 to 255
    • char can be either equivalent to signed char or unsigned char, depending on the compiler
    Then why did I get the same exact outputs for the signed and unsigned char variables inside a for-loop that would print 0 to (the last non-repeated symbol, which was 255 for both)?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Let's assume an 8-bit byte with a two's complement signed representation. Then:
    • When you assigned -140 to signed char, -140 is outside of the range of signed char, so what happened is that it was converted by adding 256 (i.e., 2^n, where ^ denotes exponentiation and n is the number of bits), resulting in the value of 116.
    • When you assigned -140 to signed char, -140 is outside of the range of unsigned char, so what happened is that it was converted by adding 256, resulting in the value of 116.

    As you can see, you ended up with the same value.

    Quote Originally Posted by Nwb
    Then why did I get the same exact outputs for the signed and unsigned char variables inside a for-loop that would print 0 to (the last non-repeated symbol, which was 255 for both)?
    This program demonstrates that what you're claiming is false:
    Code:
    #include <iostream>
    
    int main()
    {
        for (int i = 0; i < 256; ++i)
        {
            signed char x = (signed char)i;
            unsigned char y = (unsigned char)i;
            std::cout << i << ": " << (int)x << " | " << (int)y << '\n';
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2018
    Posts
    217
    Quote Originally Posted by laserlight View Post
    Let's assume an 8-bit byte with a two's complement signed representation. Then:
    • When you assigned -140 to signed char, -140 is outside of the range of signed char, so what happened is that it was converted by adding 256 (i.e., 2^n, where ^ denotes exponentiation and n is the number of bits), resulting in the value of 116.
    • When you assigned -140 to signed char, -140 is outside of the range of unsigned char, so what happened is that it was converted by adding 256, resulting in the value of 116.

    As you can see, you ended up with the same value.


    This program demonstrates that what you're claiming is false:
    Code:
    #include <iostream>
    
    int main()
    {
        for (int i = 0; i < 256; ++i)
        {
            signed char x = (signed char)i;
            unsigned char y = (unsigned char)i;
            std::cout << i << ": " << (int)x << " | " << (int)y << '\n';
        }
    }
    Thanks a lot!!!!!!!! So essentially regardless of what identifier I have given the char, it would behave the same?

    What would be the significance of using signed or unsigned identifier on char then?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Nwb
    So essentially regardless of what identifier I have given the char
    char, signed char, and unsigned char are three distinct types.

    Quote Originally Posted by Nwb
    it would behave the same?
    No. Compile and run this program:
    Code:
    #include <iostream>
    
    int main()
    {
        signed char x = (signed char)-1;
        std::cout << (x ^ 1) << " | " << (x >> 1) << std::endl;
    }
    Then compile and run this program:
    Code:
    #include <iostream>
    
    int main()
    {
        unsigned char x = (unsigned char)-1;
        std::cout << (x ^ 1) << " | " << (x >> 1) << std::endl;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2018
    Posts
    217
    Quote Originally Posted by laserlight View Post
    char, signed char, and unsigned char are three distinct types.


    No. Compile and run this program:
    Code:
    #include <iostream>
    
    int main()
    {
        signed char x = (signed char)-1;
        std::cout << (x ^ 1) << " | " << (x >> 1) << std::endl;
    }
    Then compile and run this program:
    Code:
    #include <iostream>
    
    int main()
    {
        unsigned char x = (unsigned char)-1;
        std::cout << (x ^ 1) << " | " << (x >> 1) << std::endl;
    }
    what is ^ and >>?
    Does operation with char (single character constant) have any applications?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Nwb
    what is ^ and >>?
    Bitwise operators for exclusive or and right shift. I used them to illustrate that if you're working with bytes and their bits, you might want to explicitly work with unsigned char, unless you know it doesn't matter.

    Quote Originally Posted by Nwb
    Does operation with char (single character constant) have any applications?
    Obviously, you use them for C-style strings
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Difference between signed and unsigned integers
    By Messi 10 in forum C Programming
    Replies: 6
    Last Post: 11-25-2012, 08:26 PM
  2. Replies: 4
    Last Post: 07-24-2012, 10:41 AM
  3. cast unsigned char* to (the default) signed char*
    By Mario F. in forum C++ Programming
    Replies: 24
    Last Post: 07-27-2007, 10:41 AM
  4. Difference between unsigned and signed numbers...
    By Nutshell in forum C Programming
    Replies: 8
    Last Post: 01-26-2002, 09:48 AM
  5. DIfference between signed and unsigned integers ?
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 01-15-2002, 09:27 AM

Tags for this Thread