The difference between Char, signed char, unsigned char

Source: Internet
Author: User

ANSI C provides 3 types of characters, namely Char, signed char, unsigned char
Char is equivalent to signed char or unsigned char, but it depends on the compiler!

These three character types are stored in 1 bytes and can hold 256 different values.
The difference is the range of values
Signed char value range is 128 to 127
unsigned char value range is 0 to 255
The highest bit of signed Char is the sign bit, so char can represent -128~127, unsigned char has no sign bit, so it can represent 0~255.


But what exactly is char equivalent to signed char or unsigned char??
This is the difference between char and int!
int==signed int, but Char cannot simply think ==signed Char

To determine exactly what char is equivalent to testing based on a different compiler
Most machines use the complement to store integers, and all of the 1 bits stored in these machines by integer type are 1
Assuming my machine is so stored, I can tell if Char is equal to signed char or unsigned char.
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, because byte has no symbolic bit.

If it is char, then the system considers the highest level to be the sign bit, and 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, 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

Static Get_utili (const char *p)
{ int util;
...
while (Isspace ((int) *p)) //Skip Spaces
++p;
Util = (int) *p++;
...
}
Phenomena & Consequences:

When the incoming parameter P points to content such as 0x9a, 0XAB and so on (the highest bit is 1), the resulting int variable util value will be an error, because char expands the symbol so that the 0x9a (154 in decimal) becomes-102. Causes a data processing error when the program is running.

Bug Analysis:

The char symbol extension is compiler-dependent, but on the x86 platform, Char is always symbol-extended for any major compilation platform. When the above code assigns the char type *p to the int variable util, the conversion from char to unsigned char is required to avoid symbolic expansion according to the highest bit of char.
The symbol extension process for the above error code is as follows:
Because the short data type to be expanded is the signed number-char x=10011100b (that is, 0x9a)
Thus, when int y= (int) x is a symbol extension, that is, the sign bit of the short data type fills the high byte bit of the Long data type (the part that is more than the short data type), the value of Y is 11111111 10011100b (which becomes the decimal-102);
However, the short data type that will be expanded becomes an unsigned number after--unsigned char x=10011100b (that is, 0x9a)
when int y= (int) x is expanded by 0, that is, the high-byte bits of the Long data type are populated with zero, the value of y should be 00000000 10011100b (154 in decimal).

Correct code:
Util = (int) *p++;
Util = (int) (unsigned char) *p++

Char, signed char, unsigned char difference

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.