C : scanf or wscanf, I can't print correctly the accented character

Viewed 126

I'm trying to printf a character/String given by the user with accents, but this doesn't work. After consulting a lot of forums, I think that <wchar.h> can solve my problem, but I have the same result.

main() {
    setlocale(LC_ALL, "");

    char buffer[50], choix;

    printf("CHAR\nEntrez un char : ");
    scanf(" %c", &choix);
    printf("%c\n", choix);
    getchar();

    printf("Entrez votre éùàç prénom : ");
    scanf("%s", buffer);
    getchar();
    printf("%s\n\n", buffer);

    wchar_t wbuffer[50], wchoix;

    printf("WCHAR\nEntrez un char : ");
    wscanf(L" %lc", &wchoix);
    wprintf(L"%lc\n", wchoix);
    getchar();

    wprintf(L"Entrez votre éùàç prénom : ");
    wscanf(L"%ls", wbuffer);
    getchar();
    wprintf(L"%ls\n\n", wbuffer);
    
    _getch();
}

The result was (on Windows 10 or Windows 11): result code

All of the 'literal printf' calls are working correctly, so what happened to the data from stdin? What am I missing?

2 Answers

So if I change the code to printf the integer value of all:

int main(void) {
    setlocale(LC_ALL, "");
    printf("%s\n\n", setlocale(LC_ALL, ""));
    char buffer[50], choix;
    int i;

    printf("CHAR\nEntrez un char : ");
    scanf(" %c", &choix);
    printf("choix user : %c -> %d\n", choix, choix);
    choix = getchar();
    printf("choix getchar : %c -> %d\n", choix, choix);

    printf("Entrez votre éùàç prénom : ");
    scanf("%s", buffer);
    printf("buffer : %s -> ", buffer);
    for (i = 0; buffer[i] != '\0'; i++)
        printf("%d ", buffer[i]);
    printf("\n");
    choix = getchar();
    printf("choix getchar : %c -> %d\n", choix, choix);
    
    wchar_t wbuffer[50], wchoix;

    printf("\n\nWCHAR\nEntrez un char : ");
    wscanf(L" %lc", &wchoix);
    wprintf(L"wchar : %lc - 0x%04x\n", wchoix, wchoix);
    choix = getchar();
    printf("choix getchar : %c -> %d\n", choix, choix);

    wprintf(L"Entrez votre éùàç prénom : ");
    wscanf(L"%ls", wbuffer);
    wprintf(L"wbuffer : %ls -> ", wbuffer);
    for (i = 0; wbuffer[i] != '\0'; i++)
        wprintf(L"0x%04x ", wbuffer[i]);
    printf("\n");
    choix = getchar();
    printf("choix getchar : %c -> %d\n", choix, choix);

    _getch();
    return 0;
}

The console show : result in cmd

If I see the CP850/CP1252 table : -126 = 130 -> é / '
10 -> LF ( \n )

in wchar, if it's Unicode 0x201a -> ' (é it's 0x00E9)

Then I receive from stdin a CP850 code and printf a CP1252 one.... There's no other solution to solve this except the SetConsoleCP/SetConsoleOutputCP from windows.h (or system("CHCP 1252))? The setlocale don't change the 'stdin' from CP850 (default win consol) to CP1252?

At the end, I will do it all in English, it's more simple. ;)


I try another thing that's seem to confirm that the setlocal don't modify the console :

cmd chcp

Same result with "setlocale(LC_ALL, ".1252");". The SetConsole seem to be unavoidable :/

In final, the better compromise I found : portable code

Using narrow or wide, this is the only way that's work.

thanks to all for the help in the reflexion ;)

Related