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;
}