type 165 if I scan 'Ñ'.
type 209.
- This shows the different character encoding of manually entered characters in your IDE.
Mark Tolonen suggested that it corresponds to OEM cp437 .
(I was originally associated with UTF-8 , I'm a little confused now ...)
IN C, you need to consider the existence of two ordering sequences for characters, which may be different:
- source character set .
- character set .
The original character set refers to the encoding used by the editing environment, that is, to the place where you usually type .c files. Your system and / or editor and / or IDE work with a special coding scheme. In this case, it seems that the encoding is UTF-8.
Thus, if you write 'Ñ' in your editor, the character c has the encoding of your editor and does not encode the target system. In this case, you are encoded as 209, which gives you 'Ñ' == 209 as true.
The execution character set refers to the encoding used in the operating system and / or the console that you use to run executable (i.e. compiled) programs. It seems obvious that the encoding is Latin 1 (ISO-8859-1).
In particular, when you type Ѕ in the console of your system, it is encoded as 165, which gives you a value of 165 when printing the value.
Since this dichotomy can always happen (or not), you should think about it and make some adjustments to avoid potential problems.
Its working when I use unsigned char and check with 165. But when I used cmd to try it, after reading the txt file, it didn’t work ...
- This means that your .txt file was written using a text editor (perhaps for your own IDE) that uses an encoding different from Latin 1 (ISO-8859-1).
Let me guess: you write your C code and text files using the same IDE, but you run programs from Windows CMD.
There are two possible solutions.
The tricky solution is that you investigate coding schemes , locality issues, and wide characters . There are no quick fixes because you need to be careful about a few subtle things.
A simple solution is to make changes to all the tools you use.
- Go to the parameters of your IDE and try to get information about the encoding scheme used to save text files (I assumed you have UTF-8, but you may have other features like LATIN 1 (or ISO-8859) - 1), UTF-16 and large, etc.):
Run the CHCP command in the CMD to get the code used by your system. This code page is a number the meaning of which is explained by my Microsoft , here:
a. OEM Code Pages
b. Windows Code Pages
with. ISO Code Pages
e. LIST OF ALL WINDOWS CODEPAGES
I think you have code page 850 or well 28591 (corresponds to Latin 1 ).
Change one of these configurations to match the other.
a. In the configuration of your IDE, in the "editor options" section, you can change the encoding to something like Latin 1 or ISO-8859-1 .
b. Or well, it is better to change the codepage in CMD using the CHCP command to match OEM 437 :
CHCP 437
Probably the solution related to changing the code page in the CMD does not always work as expected.
This is a safer solution (a.): Change the configuration of your editor.
However, it would be advisable to save UTF-8 in your editor (if this is your choice of editor), because nowadays every modern software uses UTF ( Unicode ) encoding.
New information: UTF-8 encoding sometimes uses more than 1 byte to represent 1 character. The following table shows the UTF-8 encoding for the first 256 entry points:
Note. . After a little discussion of the comments, I realized that I was wrong about UTF-8 . At least this illustrates my point: coding is not a trivial matter.
So, I have to repeat my advice to OP here: go along the simplest path and try to reach an agreement with your teacher on how to handle encoding for special characters.