C setlocale() not setting codepage #11852

Closed
opened 2026-01-31 02:59:22 +00:00 by claunia · 3 comments
Owner

Originally created by @clinton-r on GitHub (Dec 21, 2020).

Environment

Microsoft Windows [Version 10.0.19041.685]
Windows Terminal Version: 1.4.3243.0
(Also occurs in conhost)

Any other software?
Microsoft Visual Studio Community 2019 Version 16.8.3

Steps to reproduce

Compile this in Visual Studio, and run it in Windows Terminal/cmd or conhost/cmd:

#include <stdio.h>
#include <locale.h>
#include <windows.h>

int main(void)
{
UINT oldCP = GetConsoleOutputCP();

if (!setlocale(LC_ALL, ".utf-8"))
{
    printf("setlocale() failed!\n");
}

printf("old CP = %d, new CP = %d\n", (int)oldCP, (int)GetConsoleOutputCP());
printf(u8"I8Σπ\n");

SetConsoleOutputCP(oldCP);
printf("Restored CP to %d\n", (int)GetConsoleOutputCP());

return 0;

}

Expected behavior

Reference: https://docs.microsoft.com/en-us/cpp/text/locales-and-code-pages?view=msvc-160#:~:text=The%20locale%20and%20code%20page,is%20used%20for%20Japanese%20Kanji.

Expected output:
old CP = 850, new CP = 65001
I8Σπ
Restored CP to 850

Actual behavior

Actual output:
old CP = 850, new CP = 850
I8Sp
Restored CP to 850

Extra Notes

Hi,
If I use SetConsoleOutputCP(65001) instead of setlocale(LC_ALL, ".utf-8"), then it does work correctly.
Thanks!

Originally created by @clinton-r on GitHub (Dec 21, 2020). <!-- 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 I ACKNOWLEDGE THE FOLLOWING BEFORE PROCEEDING: 1. If I delete this entire template and go my own path, the core team may close my issue without further explanation or engagement. 2. If I list multiple bugs/concerns in this one issue, the core team may close my issue without further explanation or engagement. 3. If I write an issue that has many duplicates, the core team may close my issue without further explanation or engagement (and without necessarily spending time to find the exact duplicate ID number). 4. If I leave the title incomplete when filing the issue, the core team may close my issue without further explanation or engagement. 5. If I file something completely blank in the body, the core team may close my issue without further explanation or engagement. All good? Then proceed! --> <!-- This bug tracker is monitored by Windows Terminal development team and other technical folks. **Important: When reporting BSODs or security issues, DO NOT attach memory dumps, logs, or traces to Github issues**. Instead, send dumps/traces to secure@microsoft.com, referencing this GitHub issue. If this is an application crash, please also provide a Feedback Hub submission link so we can find your diagnostic data on the backend. Use the category "Apps > Windows Terminal (Preview)" and choose "Share My Feedback" after submission to get the link. Please use this form and describe your issue, concisely but precisely, with as much detail as possible. --> # Environment Microsoft Windows [Version 10.0.19041.685] Windows Terminal Version: 1.4.3243.0 (Also occurs in conhost) Any other software? Microsoft Visual Studio Community 2019 Version 16.8.3 # Steps to reproduce <!-- A description of how to trigger this bug. --> Compile this in Visual Studio, and run it in Windows Terminal/cmd or conhost/cmd: #include <stdio.h> #include <locale.h> #include <windows.h> int main(void) { UINT oldCP = GetConsoleOutputCP(); if (!setlocale(LC_ALL, ".utf-8")) { printf("setlocale() failed!\n"); } printf("old CP = %d, new CP = %d\n", (int)oldCP, (int)GetConsoleOutputCP()); printf(u8"I8Σπ\n"); SetConsoleOutputCP(oldCP); printf("Restored CP to %d\n", (int)GetConsoleOutputCP()); return 0; } # Expected behavior <!-- A description of what you're expecting, possibly containing screenshots or reference material. --> Reference: https://docs.microsoft.com/en-us/cpp/text/locales-and-code-pages?view=msvc-160#:~:text=The%20locale%20and%20code%20page,is%20used%20for%20Japanese%20Kanji. Expected output: old CP = 850, new CP = 65001 I8Σπ Restored CP to 850 # Actual behavior <!-- What's actually happening? --> Actual output: old CP = 850, new CP = 850 I8Sp Restored CP to 850 # Extra Notes Hi, If I use SetConsoleOutputCP(65001) instead of setlocale(LC_ALL, ".utf-8"), then it does work correctly. Thanks!
claunia added the Needs-TriageResolution-External labels 2026-01-31 02:59:22 +00:00
Author
Owner

@DHowett commented on GitHub (Jul 2, 2021):

Ugh! I'm sorry we missed this in our triage queue. It's now been six months.

I think that this is an issue with the CRT, which would be better to file on devcom (developer community). Unfortunately, we don't have any control over whether setlocale passes through to the console CP.

This seems, on its front, to be related to the UTF-8 issue in #4551. They probably don't want to make setlocale do that until input works properly?

/dup #4551

tagging external as well -- for visibility. sorry!

@DHowett commented on GitHub (Jul 2, 2021): Ugh! I'm sorry we missed this in our triage queue. It's now been six months. I think that this is an issue with the CRT, which would be better to file on devcom ([developer community](https://developercommunity.visualstudio.com/home)). Unfortunately, we don't have any control over whether setlocale passes through to the console CP. This seems, on its front, to be related to the UTF-8 issue in #4551. They probably don't want to make setlocale do that until input works properly? /dup #4551 tagging external as well -- for visibility. sorry!
Author
Owner

@ghost commented on GitHub (Jul 2, 2021):

Hi! We've identified this issue as a duplicate of another one that already exists on this Issue Tracker. This specific instance is being closed in favor of tracking the concern over on the referenced thread. Thanks for your report!

@ghost commented on GitHub (Jul 2, 2021): Hi! We've identified this issue as a duplicate of another one that already exists on this Issue Tracker. This specific instance is being closed in favor of tracking the concern over on the referenced thread. Thanks for your report!
Author
Owner

@eryksun commented on GitHub (Jul 2, 2021):

The decision to modify the console session lies with the application. Typically this is a user-driven setting via "chcp.com" or the registry "CodePage" value in "HKCU\Console\<session title>". It wouldn't be as much of an issue if modifying the console I/O code pages only affected the current process and child processes. As it stands, the C runtime is justified in choosing to not modify the console session automatically. Instead, if it isn't using the default "C" locale, it addresses the mismatch as best as it can via translation between locale multibyte text and console multibyte text.

@eryksun commented on GitHub (Jul 2, 2021): The decision to modify the console session lies with the application. Typically this is a user-driven setting via "chcp.com" or the registry "CodePage" value in "HKCU\\Console\\\<session title\>". It wouldn't be as much of an issue if modifying the console I/O code pages only affected the current process and child processes. As it stands, the C runtime is justified in choosing to not modify the console session automatically. Instead, if it isn't using the default "C" locale, it addresses the mismatch as best as it can via translation between locale multibyte text and console multibyte text.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#11852