简体   繁体   中英

Scanf and printf return different unicode characters

I need to make a program that reads and writes Greek letters. Since Greek is not in ASCII, I set the console locale to UTF-8. I managed to make some strings work in this multibyte mess, until i got to the part where i need user input

So,ill only post the problematic part

while(1) {
    char inputc[50]; 
    memset(inputc,'\0',50);
    scanf("%s",inputc)
    printf("%s",inputc);
}

With any greek character, this will print something different than the input. Also, if I do printf("%d",inputc[i]); for each element to get the int value (says negative for greek letters), I get a different value than reading the same character from a literal.

the command used is SetConsoleOutputCP(CP_UTF8);

That only affects stdout (printf). To set stdin (scanf) as well you would need to also SetConsoleCP(CP_UTF8) . If you set one but not the other then the input and output characters will naturally differ.

However, please be aware that there are serious bugs in the Windows Console when set to code page 65001/CP_UTF8 (or generally any multi-byte code page that doesn't have special support, ie those that aren't legacy locale-default code pages). Windows reports byte counts incorrectly in this state, leading to print calls that mangle and repeat output, and scan calls that hang. This is not generally a feasible way of getting Windows programs to talk Unicode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM