Difference between unsigned char and char

Source: Internet
Author: User

Character values of Type unsigned char have a range from 0 to 0xff hexadecimal. A signed Char has range 0x80 to 0x7f. these ranges translate to 0 to 255 decimal, and-128 to + 127 decimal, respectively. the/J compiler option changes the default from signed to unsigned.

Char is signed

Unsigned char is unsigned and contains all positive numbers.

If both are used as characters, there is no difference, but there is a difference when using integers:

Char Integer Range:-128 to 127 (0x80 _ 0x7f ),

The Integer Range of unsigned char is 0 to 255 (0 _ 0xff)

In most cases, data of the char, signed Char, and unsigned char types share the same characteristics. However, when you assign a single byte number to a large integer number field, you will see their differences in symbol extension. Another difference is that when a number between 128 and 255 is assigned to the signed Char variable, the compiler must first convert the value, and a warning will also appear.

See the following functions.

Function: counts the number of letters in a string.

Char stext [] = "12345 hello ";

Len = strlen (stext );

Int sum = 0;

For (INT I = 0; I <Len; I ++)

{

// The ASCII of character> = 65

If (stext [I]> 64)

{

Sum ++;

}

}

In this way, you can basically count any Chinese character,

Because char is signed, the maximum value is 127. If it is exceeded, it becomes a negative number. For example, if 7f is 127, 80 is-1.

At this time, you must write

Unsigned char stext [] = "12345 hello ";

ReferenceProgram:

# Include <iostream>

# Include <string. h>

Using namespace STD;

Int main ()

{

Unsigned char stext [] = "12345 hello ";

Int Len = 0;

Char temp;

// Unsigned int strlen (const char * s );

// We need to convert stext from unsigned char * to const char *

Len = strlen (const char *) stext );

Cout <"The strlen is" <Len <Endl;

Int sum = 0;

For (INT I = 0; I <Len; I ++)

{

// The ASCII of character> = 65

If (stext [I]> 64)

{

Sum ++;

}

Cout <"Character Count:" <sum <Endl;

}

// Just to have a pause

Cout <"enter any thing to exit! "<Endl;

Cin> temp;

Return 0;

}

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.