5

Where is unsigned char used in C ( please tell about some real-world examples )? Why would we need both char and unsigned char?

Char ranges from -127 to 128 ( 8-bit integer ) Unsigned Char ranges from 0 to 255 ( 8-bit integer )

If the purpose is to store ASCII, why would we need both?

Another question. Consider this:

char a = 127;
unsigned char b = 255;

When I print it using std::cout. It gives me different characters. Can you explain why? ( I'm using Microsoft vs11 compiler )

6
  • 2
    Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask
    – gnat
    May 22, 2013 at 11:30
  • @gnat I was just curious about the two data types. One thing I found was that I cannot directly create an std::string using unsigned char without using reinterpret_cast<const char *>.
    – captain
    May 22, 2013 at 11:38
  • FYI I have been searching. I did not find a real-world example where unsigned char is useful.
    – captain
    May 22, 2013 at 11:40
  • 1
    well, an unsigned will allow you to store a larger number than a signed, if you're not interested in -ve values. Imagine if you had a counter that went from 0 to 255, with unsigned its easy with signed its a bit more faff
    – PeteH
    May 22, 2013 at 11:43
  • You might find this a useful read: en.wikipedia.org/wiki/Extended_ASCII.
    – Blrfl
    May 22, 2013 at 14:00

3 Answers 3

6

Where is unsigned char used in C ( please tell about some real-world examples )? Why would we need both char and unsigned char?

signed or unsigned are properties of the different C data types (char, short, int, etc...). So it's not a question of whether we need both, both come automatically as part of how C is defined.

Char ranges from -127 to 128 ( 8-bit integer ) Unsigned Char ranges from 0 to 255 ( 8-bit integer )

While that's true for most of the platforms out there, there's nothing garanting that char will be signed. In fact on ARM platforms, it's unsigned. See this fix: http://www.spinics.net/lists/linux-btrfs/msg20653.html as an example of real-world bug introduced by assuming that char is signed.

If the purpose is to store ASCII, why would we need both?

The thing is, the purpose is not to store ASCII. It happens to be used to store ASCII but it's a not a necessity.

char a = 127;

unsigned char b = 255;

When I print it using std::cout. It gives me different characters. Can you explain why? ( I'm using Microsoft vs11 compiler

I think what you're looking for is the following:

#include <iostream>

int main(void)
{
        char a = -1; 
        unsigned char b = 255;
        std::cout << a << std::endl << b << std::endl;
        return 0;
}

That is '-1' signed will be equal to '255' unsigned. Please note that this is heavily implementation dependent, and there's nothing that guarantees it will work accross all platforms and compilers.

7

unsigned char stores a number. You can use it when you need to represent numbers in the range 0 to 255 and want to conserve memory.

4

You forgot to mention signed char, which also exists and is a distinct from char.

char has the same range as either signed char or unsigned char and its intended use if for character data (although you can only use narrow encodings, like ASCII, if you want the correspondence of 1 char == 1 character).

Despite the use of char in their names, both signed char and unsigned char are not targeted for character data, but rather to hold small numbers. unsigned char in particular is often used to store raw byte data.

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.