Home c Why ASCII Cyrillic Symbols are negative in SI?

Why ASCII Cyrillic Symbols are negative in SI?

Author

Date

Category

Why in the Windows console program in SI gives a negative ASCII code of a symbol from Cyrillic?
I understand that the Cyrillic Cyrillic code goes beyond 127, and therefore using Unsigned Char. But somehow does not help.

Small pieces of program:

int main ()
{
  char * wrd_replace_from = null;
  SetConsoleCP (1251); // Setting the code page WIN-CP 1251 in the input stream
  SetConsoleoutPutcp (1251); // Setting the code page WIN-CP 1251 in the output stream
  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' & amp; & amp; c! = null)
    PrintF_s ("% C", C);
  Return C;
}
char * inputwordreplacefrom ()
{
  char * str = (char *) Malloc (SizeOf (Char)); // pointer to the first element of the new line
  INT LENGTH = 0; // Character number counter
  Printf ("Maximum number of characters:% d. You can enter only the letters of the Russian and English alphabet. Enter the word", maxlengthline);
  While ((Length & lt; maxlengthhline) & amp; & amp; ((* (str + length) = getchr ())! = '\ n') & amp; & amp; (* (str + length)! = NULL))
  {
    PrintF ("\ N str =% d \ n", (* (str + length)));
    if (
      (64 & lt; * (str + length) & amp; & amp; * (STR + LENGTH) & lt; 91)
      || (96 & lt; * (STR + LENGTH) & amp; & amp; * (STR + LENGTH) & lt; 123) ||
      (191 & lt; * (str + length) & amp; & amp; * (STR + LENGTH) & lt; 256) ||
      (* (STR + LENGTH) == 168) || (* (STR + LENGTH) == 184)
      )
    {
      Length ++;
      if (Length == maxlengthline)
      {
        Printf ("\ N \ t ------ information \ n");
        Printf ("\ T ------ The maximum string length is exceeded. Enter completed.");
      }
    }
    ELSE.
    {
      Printf ("\ N eros. You can enter only the letters of the Russian and English alphabet. \ n");
      If (Length)
      {
        Length--;
        PrintF ("\ b \ b");
      }
    }
    str = (char *) realloc (str, (Length + 2) * Sizeof (Char));
  }
  // Tag: End of the string of characters
  * (STR + Length) = '\ 0';
  Return str;
}

Answer 1, Authority 100%

You work on the platform on which the type char is icon . This means that with the type of char in 8 bits, it will traditionally have a range -128..127 . Therefore, it is obvious that your comparisons of char values ​​with numbers of type 191 , 168 , etc. It makes no sense and can not.

For this reason, “do not work” your conditions in if . For the same reason, your printf prints negative values.

If you want at the level of your code to work with characters codes in the 0..255 range, then either explicitly manually give all values ​​like CHAR unsigned CHAR , or rot on the compiler settings to make the type char unsigned by itself.


bewilderment also causes the expression (* (STR + LENGTH)! = NULL) . What was meant here? null – constant intended for use in index contexts, i.e. Its, for example, can be compared with pointers. You suddenly compare with Char .

Looking more attentively, my temper eye noticed that the value of the type char allegedly equal to null can really come back from the handwritten function Getchr At first it seemed that this is the standard getchar ). But it does not make the situation better. null cannot be assigned to char or compare with char .


Answer 2

There is a format problem in the call to the printf function. “% d” – displays a signed integer. Unsigned outputs the “% u” modifier, but by default it outputs the type int , which is usually 4-bytes. To display only one byte before “u” you need to put the modifier “hh”. As a result, the line should look like this:

printf ("\ n str =% hhu \ n", (* (str + length)));

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions