Introduction to the differences between C char and unsigned char

  • 2020-04-01 21:35:20
  • OfStack

In C, the default underlying data type is signed, so let's take char as an example to illustrate the difference between (signed) char and unsigned char.

First of all, in memory, char is no different from unsigned char, it's all one byte, the only difference is that the highest bit of char is the signed bit, so char can represent -127~127,unsigned char has no signed bit, so it can represent 0~255, that's easy to understand, 8 bits, up to 256 cases, so it can represent 256 Numbers anyway.

What is the difference in actual use? Mainly symbol bits, but in normal assignment, read and write files and network byte streams are no different, it is just a byte, no matter what the highest bit is, the final result is the same, just how you understand the highest bit, may not be the same on the screen.

The big difference is that we found that byte used unsigned char. Why? First of all, we generally understand that byte has no symbolic bits. More importantly, the system does some extra work when assigning byte values to data types such as int and long. If it's a char, then the system thinks that the highest bit is a signed bit, and an int might be 16 or 32 bits, then the highest bit is extended (note that assigning an unsigned int also extends) and not if it's an unsigned char. If the highest bit is 0, there is no difference, if it is 1, there is a difference. You can also derive other types of short, unsigned short, and so on.

You can see the difference with the following small examples

The include < Stdio. H>

Void f (unsigned char v)
{
Char c = v;
Unsigned char uc = v;
Unsigned int a = c, b = uc;
Int I = c, j = uc;
Printf (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - \ n ");
Printf ("%%c: %c, %c\n", c, uc);
Printf ("%%X: %X, %X\n", c, uc);
Printf ("%%u: %u, %u\n", a, b);
Printf ("%%d: %d, %d\n", I, j);
}

Int main(int argc, char *argv[])
{
F (0 x80);
F (0 x7f);
Return 0;
}


The output is as follows:
 
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201304/201304190950472.png ">

Result analysis:

For (signed)char, 0x80 is represented as 1000 0000 in binary, and when it is assigned to an unsigned int or int as a char, the system considers the highest bit to be a signed bit and extends the highest bit. 0x7F, on the other hand, is represented as 0111 1111 in binary, with the highest bit being 0 and not extended.

For unsigned char, whether the highest bit is 0 or 1, there is no extension.


Related articles: