The value of negative char values ​​in C

  • characters in 'C' are used to represent characters.
  • Numbers representing characters on all pages of the code are always positive.

What is the use of signed characters? Are negative values ​​contained in characters used only as integral values ​​in a smaller integral data type than int and short? They have no other interpretation? (For example, positive values ​​in characters representing characters)

+5
source share
8 answers

characters in 'C' are used to represent characters.

, c .

+11

, , .

Erm... !?

C99 :

char, .

, . char !

+4

(C99, 6.5.2 §3):

, char, . char, . - char, , .

"plain" char signed char unsigned char: signed char unsigned char - , (C99, 6.5. 2 §5):

, , char, , '' Plain char.

+4

:

char SCHAR_MIN SCHAR_MAX, limits.h. SCHAR_MIN -127 ( ), SCHAR_MAX 127 . , , , SCHAR_MIN -128, .

, , "" signed char. , , , , ASCII 0-127. signed char 0-127 - , 0 .

, , - ASCII , , , , . -, .

+2

.

char buf[10000];
fgets(buf, sizeof buf, stdin);
unsigned charcount[UCHAR_MAX] = {0};
char *p = buf;
while (*p) {
    charcount[*p]++; /* if (*p < 0) BOOM! */
    // charcount[(unsigned char)*p]++;
    p++;
}
+2

, char char, char.

+1

C ++ . A char . :

  • , . 8- . .
  • , , 100K, , , . .

C . '0' 48.

0

C a char ( signed char unsigned char) , C 8 .

, . (0..255 unsigned, -127..127 [1]) , , , , ints, .

. - , , . , char C.

[1] -128..127 , C .

0

All Articles