C ++ char, signed Char, unsigned char

Source: Internet
Author: User

 

Ansi c provides three types of characters: Char, signed Char, and unsigned char. There are only two types of characters like short and INT (INT is unsigned int by default ).

Each of the three occupies 1 byte. Therefore:

The value range of signed Char is-128 to 127 (with signs)
Unsigned char value range: 0 to 255

Everyone knows this !!

But what about Char? What is the scope?

The answer is: not necessarily !!!

Let's take a look at what masters say:

(Thinking in C ++ 2nd ):

Signed is the default and is only necessary with Char;
Char may or may not default to signed. By specifying signed Char, you force the sign bit to be used.

Note: The signed type is the default (for other integer types such as INT) and is only required for char. Char may be signed or unsigned (I think this may depend on the specific implementation of the compiler ). But by explicitly specifying a char as signed, you force it to become a signed character type.

My opinion is:

Let's start with the usefulness of these types!

Char is used to declare characters!

Signed char and unsigned char are used to declare values, which are the same as int and unsigned int, it only occupies less space (this is especially effective in embedded systems with limited mobile phones and other spaces !), Indicates a limited range.

How is char implemented in various compilers?

In the c standard, this is Impementation Defined, that is, it is not clearly Defined by the specific compiler.

However, generally, signed char or unsigned char is used to implement char, because these three types of objects have the same representation in the storage media (they are all 8bit 01 strings, but it is different when parsing ).

As for whether it is signed char or unsigned char, the compilers are different !! The VC compiler and GCC on x86 define char as signed char, while arm-linux-gcc defines char as unsigned char.

In this way, there will be problems with code porting (a problem that troubles our programmers). The simplest example is as follows:

Char a = 0xb6;

If (a = 0xb6) puts ("hello world! ");

Hello world is not printed on both vc and x86 gcc! .

Compiled using arm-linux-gcc. On the arm board, you can print hello world! .

Let's change the following:

Char a = 0xb6;
Short B = 0xb600;
Int c = 0xb6000000;

If (a = 0xb6) puts ("");
If (B = 0xb600) puts ("B ");
If (c = 0xb6000000) puts ("c ");

On the GCC of VC or x86, only C is printed. Compiled using ARM-Linux-GCC. On the arm board, A and C can be printed. Did you find something?

First, we will introduce integer promotion ). Generally speaking, C will automatically upgrade to int when processing INTEGER (char short INT) (if the int range is not enough, it will be upgraded to unsigned INT ). For example, "A = 0xb6", 0xb6 is processed as an int and changed to 0x000000b6 (for constants, we will also explain it carefully later ). A is upgraded to int. If char is defined as conforming, A is a negative number. Because the highest bit is 1, A is upgraded to 0xffffffb6. If char is defined as unsigned, A is upgraded to 0x000000b6.
.

That is, on the GCC of VC or x86, (a = 0xb6) will change to (0xffffffb6 = 0x000000b6), while on the arm-Linux-GCC, change to (0x000000b6 = 0x000000b6 ).

For short, because the C standard clearly specifies that no keyword is added, it indicates the number of symbols. Therefore, no matter which compiler B = 0xb600 will become 0xffffb600 = 0x0000b600.

Int itself is an int, so integer promotion is not needed. Therefore, in C = 0xb60000, C does not perform any processing and is read directly from the memory, that is, 0xb60000 = 0xb60000.

Finally, let's briefly talk about constants. Constants starting with octal (0) or hexadecimal (starting with 0x) are processed as unsigned numbers! In addition, for example, char a = 0xb6; there are two Implementation Defined statements. One is char with no symbols, and the other is. If char is signed, 0xb6 will be processed when int 0x000000b6, changing this int to a signed char has an overflow problem. The 0xb6 value is a positive number and is assigned to a, but it turns to a negative number. How can this problem be solved? This is also Implementation Defined by c.

FR: http://hi.baidu.com/nicker2010/blog/item/11eb1a496147743a08f7efad.html

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.