c - Difference between signed / unsigned char - Stack Overflow
c++ - Char and unsigned char - Software Engineering Stack Exchange
c++ - What is an unsigned char? - Stack Overflow
What's difference between unsigned char and signed char ?
Videos
I don’t understand the use of unsigned char, I know c++ use number to store letter according the ASCII table, but the table is ranged from 0 to 127, so it wouldn’t make sense to have a have more number like unsigned char which is 0 to 255. I know default char is signed which is -128 to 127. Also could anyone give me an example code on the usage of 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.
I slightly disagree with the above. The unsigned char simply means: Use the most significant bit instead of treating it as a bit flag for +/- sign when performing arithmetic operations.
It makes significance if you use char as a number for instance:
typedef char BYTE1;
typedef unsigned char BYTE2;
BYTE1 a;
BYTE2 b;
For variable a, only 7 bits are available and its range is (-127 to 127) = (+/-)2^7 -1.
For variable b all 8 bits are available and the range is 0 to 255 (2^8 -1).
If you use char as character, "unsigned" is completely ignored by the compiler just as comments are removed from your program.
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.
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.
In C++, there are three distinct character types:
charsigned charunsigned char
1. 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 isint) - 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.
2. signed char/ 3. unsigned char
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. This might be useful for displaying an octet e.g. as hex value.
"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.
This is implementation dependent, as the C standard does NOT define the signed-ness of char. Depending on the platform, char may be signed or unsigned, so you need to explicitly ask for signed char or unsigned char if your implementation depends on it. Just use char if you intend to represent characters from strings, as this will match what your platform puts in the string.
The difference between signed char and unsigned char is as you'd expect. On most platforms, signed char will be an 8-bit two's complement number ranging from -128 to 127, and unsigned char will be an 8-bit unsigned integer (0 to 255). Note the standard does NOT require that char types have 8 bits, only that sizeof(char) return 1. You can get at the number of bits in a char with CHAR_BIT in limits.h. There are few if any platforms today where this will be something other than 8, though.
There is a nice summary of this issue here.
As others have mentioned since I posted this, you're better off using int8_t and uint8_t if you really want to represent small integers.