What Is an Unsigned Char

What is an unsigned char?

In C++, there are three distinct character types:

  • char
  • signed char
  • unsigned char

If you are using character types for text, use the unqualified char:

  • it is the type of character literals like 'a' or '0' (in C++ only, in C their type is int)
  • it is the type that makes up C strings like "abcde"

It also works out as a number value, but it is unspecified whether that value is treated as signed or unsigned. Beware character comparisons through inequalities - although if you limit yourself to ASCII (0-127) you're just about safe.

If you are using character types as numbers, use:

  • signed char, which gives you at least the -127 to 127 range. (-128 to 127 is common)
  • unsigned char, which gives you at least the 0 to 255 range.

"At least", because the C++ standard only gives the minimum range of values that each numeric type is required to cover. sizeof (char) is required to be 1 (i.e. one byte), but a byte could in theory be for example 32 bits. sizeof would still be report its size as 1 - meaning that you could have sizeof (char) == sizeof (long) == 1.

Difference between signed / unsigned char

There's no dedicated "character type" in C language. char is an integer type, same (in that regard) as int, short and other integer types. char just happens to be the smallest integer type. So, just like any other integer type, it can be signed or unsigned.

It is true that (as the name suggests) char is mostly intended to be used to represent characters. But characters in C are represented by their integer "codes", so there's nothing unusual in the fact that an integer type char is used to serve that purpose.

The only general difference between char and other integer types is that plain char is not synonymous with signed char, while with other integer types the signed modifier is optional/implied.

Why do C++ streams use char instead of unsigned char?

Possibly I've misunderstood the question, but conversion from unsigned char to char isn't unspecified, it's implementation-dependent (4.7-3 in the C++ standard).

The type of a 1-byte character in C++ is "char", not "unsigned char". This gives implementations a bit more freedom to do the best thing on the platform (for example, the standards body may have believed that there exist CPUs where signed byte arithmetic is faster than unsigned byte arithmetic, although that's speculation on my part). Also for compatibility with C. The result of removing this kind of existential uncertainty from C++ is C# ;-)

Given that the "char" type exists, I think it makes sense for the usual streams to use it even though its signedness isn't defined. So maybe your question is answered by the answer to, "why didn't C++ just define char to be unsigned?"

How can I confirm the range of unsigned char in C?

First of all, when you pass an integer argument smaller than int to a variadic function (like printf) then it's automatically promoted to an int.

Secondly, when you use arithmetic, smaller integer types again are promoted to int.

Read e.g. this implicit conversion reference for more information.

If you want to print the char (or rather unsigned char) value you need to use the hh prefix for the format specifier (see e.g. this printf and family reference):

printf("%hhu\n", (unsigned char) (a - 1));

Which datatype is used for unsigned char in C#?

The equivalent of unsigned char in C# is byte.

byte getValue(string s)
{

}

Second option: if you use unsigned char as a character storage, you should use char:

char getValue(string s)
{

}

You have to remember, that C++ treats characters and 8-byte values equally. So, for instance in C++:

'A' + 'B' == 65 + 66 == 65 + 'B' == 'A' + 66

You have to check from the context, whether the unsigned char is a character or a number. From string being passed as a parameter, one can guess, that the function processes characters so mostly probably you want a char C# type.



Related Topics



Leave a reply



Submit