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.
c++ - What is an unsigned char? - Stack Overflow
c - Difference between signed / unsigned char - Stack Overflow
What is the use of unsigned char?
c++ - Char and unsigned char - Software Engineering Stack Exchange
Videos
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.
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.
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
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.
Declare your ch as
unsigned char ch = 212 ;
And your printf will work.
This is because in this case the char type is signed on your system*. When this happens, the data gets sign-extended during the default conversions while passing the data to the function with variable number of arguments. Since 212 is greater than 0x80, it's treated as negative, %u interprets the number as a large positive number:
212 = 0xD4
When it is sign-extended, FFs are pre-pended to your number, so it becomes
0xFFFFFFD4 = 4294967252
which is the number that gets printed.
Note that this behavior is specific to your implementation. According to C99 specification, all char types are promoted to (signed) int, because an int can represent all values of a char, signed or unsigned:
6.1.1.2: If an
intcan represent all values of the original type, the value is converted to anint; otherwise, it is converted to anunsigned int.
This results in passing an int to a format specifier %u, which expects an unsigned int.
To avoid undefined behavior in your program, add explicit type casts as follows:
unsigned char ch = (unsigned char)212;
printf("%u", (unsigned int)ch);
* In general, the standard leaves the signedness of
char up to the implementation. See this question for more details.