The essential difference between char and unsigned char

Source: Internet
Author: User

Source: http://www.cnblogs.com/qytan36/archive/2010/09/27/1836569.html

Reference: http://bbs.csdn.net/topics/350066482

In C, the default base data type is signed, and now we use 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 sign bit, so char can represent -128~127, unsigned char has no sign bit, so it can represent 0~255, this good understanding , 8 bit, up to 256 cases, so it can represent 256 numbers anyway.

What is the difference between the actual use of the process?

Mainly sign bit, but in the ordinary assignment, read and write files and network byte stream are no different, anyway is a byte, regardless of the highest bit is what, the final reading results are the same, but how you understand the highest bit, the screen above the display may not be the same.

But we find that when we represent byte, we use unsigned char, which is why?

First of all, we generally understand that byte has no symbolic bit, and more importantly, if the value of byte is assigned to a data type such as Int,long, the system does some extra work.

If it is char, then the system considers the highest level to be the sign bit, and the int may be 16 or 32 bits, then the top bit is extended (note that the assignment to unsigned int also expands)

And if it is unsigned char, then it will not expand.

This is the biggest difference between the two.

In the same vein, other types can be deduced, such as short and unsigned short. Wait a minute

Specific can be seen by the following small example of the difference

#include <stdio.h>voidF (unsignedCharv) {Charc =v; unsignedCharUC =v; unsignedintA = c, B =UC; inti = 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); } intMainintargcChar*argv[]) {F (0x80); F (0x7F); return 0;}

Output Result:

----------------

%c:?,?

%X:FFFFFF80, 80

%u:4294967168, 128

%d:-128, 128

----------------

%c:,

%x:7f, 7F

%u:127, 127

%d:127, 127

Thus, if the highest bit is 0 o'clock, there is no difference between the two, if it is 1 o'clock, then there is a difference.

2014-07-02

The essential difference between char and unsigned char

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.