0

I'm getting to know the programming language C and I'm having some problems with encoding. When I write to Command Prompt via printf non-ASCII characters are display incorrectly. The source code file encoding is UTF-8.

Source code:

#include <stdio.h>

int main(void)
{
    printf("\n\u2212\n");

    return 0;
}

cl.exe:

warning C4566: character represented by universal-character-name '\u2212' cannot be represented in the current code page (1252)

How do I get Command Prompt to correctly display Unicode?

HelloWorld
  • 2,375
  • 5
  • 22
  • 21
  • I've never had to specify encoding. – user1231232141214124 Jun 30 '14 at 23:05
  • Give some example how you try to output these characters, what you are expecting and what you get. And maybe give the source file encoding (if not 7-bit ASCII) and the encoding of your terminal. – mafso Jun 30 '14 at 23:34
  • Yes, [this codepage](https://en.wikipedia.org/wiki/Windows-1252) cannot encode the Unicode code point 0x2212. Also note, that `stdout` is a text stream rather than a binary stream, so you should use `printf("\n\u2212\n");`. – mafso Jun 30 '14 at 23:48
  • You don't have a C question, actually (as far as I can see); it's a question about how to change the locale/encoding in Windows. Maybe re-tag your question (to reach an audience more likely being able to help you) or ask your OS vendor. – mafso Jun 30 '14 at 23:58
  • Actually, I meant adding a tag to reach a Windows-audience, not removing the C tag :) – mafso Jul 01 '14 at 00:05

2 Answers2

0

You are probably outputting a correct symbol the problem is the command-line don't understand the character to display.Ensure that your terminal can handle UTF-8 output.Try to Setup your local correctly.

user3403531
  • 11
  • 1
  • 3
0

Try this:

#include <stdio.h>

int main(void)
{
    printf("\r\n%C\r\n", L'\u2212'); // or %Lc
    return 0;
}

Or this:

#include <stdio.h>

int main(void)
{
    wprintf(L"\r\n\u2212\r\n");
    return 0;
}

Or this:

#include <stdio.h>

int main(void)
{
    wprintf(L"\r\n%c\r\n", L'\u2212');
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • This won't help. OP has a console where 0x2212 don't have an encoding. This is not a C problem but a question about how to change the encoding of the Windows “console”. – mafso Jun 30 '14 at 23:55
  • Although I agree that the Windows console cannot handle Unicode very well, he is getting a compiler warning, not a console warning. So deal with the compiler first, then deal with the console second. – Remy Lebeau Jun 30 '14 at 23:57
  • There's a source character set and a execution character set in C; OP obviously has a problem converting the former to the latter. And the latter (Windows-1252) don't have a representation for 0x2212, so there is no solution to fix that without changing this character set. I don't know much about Windows, but the code you gave seems very unlikely to behave better than OP's original code (edit: At least the first snippet; maybe the Windows console supports wide characters in a different encoding…). – mafso Jul 01 '14 at 00:02