Introduction of the difference between C-language char and unsigned char _c language

Source: Internet
Author: User

In C, the default underlying data type is signed, and now we take char as an example to illustrate the difference between (signed) Char and unsigned char.

First in memory, Char is no different from unsigned char, it is a byte, the only difference is that the highest bit of char is the symbol bit, so char can represent -127~127,unsigned char without symbol bit, so it can represent 0~255, this good understanding , 8 bit, up to 256, so it can represent 256 digits in any case.

What is the difference between the actual use of the process? Mainly is the symbol bit, but in the ordinary assignment, reads and writes the file and the network Word throttling all does not have any difference, anyway is a byte, regardless the highest bit is what, the final reading result all is same, only how you understand the highest position, in the screen above display may not be same.

The biggest difference between the two is: but we find that when we represent byte, we use unsigned char. First of all, we generally understand that byte has no symbolic bits, and more importantly, if you assign a byte value to a data type such as Int,long, the system does some extra work. If it is char, then the system thinks the highest position is the sign bit, and the int may be 16 or 32 bits, then the highest bit is extended (note that the unsigned int is also extended) and if it is unsigned char, it does not expand. If the highest position is 0 o'clock, there is no difference between the two, if 1 o'clock, then there is a difference. In the same vein, other types can be deduced, such as short, unsigned, and so on.

The difference can be seen by the following small example

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 (0x80);
f (0x7F);
return 0;
}


The resulting output is as follows:

Results Analysis:

for (signed) Char, 0x80 is represented as 1000 0000 in binary notation, and when it is assigned as char to unsigned int or int, the system considers the highest level to be the symbol bit and extends the top bit. The 0x7f is represented as 0111 1111, the highest bit is 0, and does not expand.

For unsigned char, no extension is made, regardless of the highest bit 0 or 1.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.