Why is ascii the Cyrillian symbols negative in C?
-
Why does a program in the Windows console in the C-language produce a negative ascii-code symbol from the Cyrillians? I understand that the code ascii of Cyrillians goes beyond 127, so I use unsigned char. But it doesn't work.
Small pieces of programme:
int main() { char *word_replace_from = NULL;
SetConsoleCP(1251); // установка кодовой страницы win-cp 1251 в поток ввода SetConsoleOutputCP(1251); // установка кодовой страницы win-cp 1251 в поток вывода word_replace_from = inputWordReplaceFrom();
}
char getchr()
{
unsigned char c;
c = _getch();if (c == KEY_SAVE_EXIT) c = NULL; if (c == '\r') c = '\n'; if (c != '\b' && c != NULL) printf_s("%c", c); return c;
}
char *inputWordReplaceFrom()
{char *str = (char*) malloc(sizeof(char)); // указатель на первый элемент новой строки int length = 0; // счётчик количества символов printf("Максимальное количество символов: %d. Вводить можно только буквы русского и английского алфавита. Введите слово ", MAXLENGTHLINE); while ((length < MAXLENGTHLINE) && ((*(str + length) = getchr()) != '\n') && (*(str + length) != NULL)) { printf("\n str = %d \n", (*(str + length))); if ( (64 < *(str + length) && *(str + length) < 91) || (96 < *(str + length) && *(str + length) < 123) || (191 < *(str + length) && *(str + length) < 256) || (*(str + length) == 168) || (*(str + length) == 184) ) { length++; if (length == MAXLENGTHLINE) { printf("\n \t ------ Информация \n "); printf("\t ------Превышена максимальная длина строки. Ввод завершен. "); } } else { printf("\nОшибка. Можно вводить только буквы русского и английского алфавита.\n"); if (length) { length--; printf("\b \b"); } } str = (char*) realloc(str, (length + 2) * sizeof(char)); } // Метка: конец строки символов *(str + length) = '\0'; return str;
}
-
You're working on a platform where the guy is.
char
Yes signs♪ It means that in the width of the typechar
8th battle, it will traditionally have a range-128..127
♪ So it's obvious that your comparisons of the type valueschar
Numbers of type191
♪168
and so on, it doesn't make sense.That's why they don't work.
if
♪ That's the same reason you are.printf
printing negative values.If you want to work at your code level with the symbol codes in the range.
0..255
either clearly manually releasing all type valueschar
typeunsigned char
♪ Or you're in the building of a compilator to make a type ♪char
I don't know.Inconsistency also causes expression
(*(str + length) != NULL)
♪ What was that supposed to mean?NULL
- Constant for use decrees contexts, i.e., it can be compared to the indexes. You've got it all right.char
♪Looking more carefully, my eyes have noticed that it's like
char
allegedly equalNULL
can really come back from a manuscript function.getchr
(I first thought it was standardgetchar
) But that doesn't make things better.NULL
type values may not be assignedchar
or compare to type valueschar
♪