How to printf accented characters in ANSI C (like á é í ó ú)

后端 未结 3 1107
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-10 13:50

I tried to printf with some accented characters such as á é í ó ú:

printf(\"my name is Seán\\n\");

The text editor in

3条回答
  •  死守一世寂寞
    2020-12-10 14:26

    Windows console is generally considered badly broken regarding to character encodings. You can read about this problem here, for example.

    The problem is that Windows generally uses the ANSI codepage (which is assuming you are in Western Europe or America Windows-1252), but the console uses the OEM codepage (CP850 under the same assumption).

    You have several options:

    • Convert the text to CP850 before writing it (see CharToOem()). The drawback is that if the user redirects the output to a file (> file.txt) and opens the file with e.g. Notepad, he will see it wrong.
    • Change the codepage of the console: You need to select a TTF console font (Lucida Console, for example) and use the command chcp 1252.
    • Use UNICODE text and wprintf(): You need the TTF console font anyway.

提交回复
热议问题