非英文Windows10环境下C语言printf无法输出US-ASCII反斜杠求助
Hey there, let's work through this problem together. The root cause here is that your non-English Windows 10's default console code page doesn't match the UTF-8/ASCII encoding you're using for your source file, which is causing the backslash (\) to render as that weird W-like character. Here are the most effective fixes:
1. Force Console Output to Use UTF-8 via Code
The quickest fix is to add a Windows API call to set your console's output encoding directly in your code. This ensures consistency regardless of system settings:
#include <stdio.h> #include <windows.h> // Required for console encoding functions int main(void) { // Set console output to UTF-8 to match your source file encoding SetConsoleOutputCP(CP_UTF8); printf("Use \\n to cause a newline; use \\t to cause a tab.\n"); return 0; }
Compile and run this, and your backslashes should display correctly now.
2. Adjust System Console Default Settings
If you want to fix this system-wide for all console apps:
- Open Command Prompt (cmd.exe), right-click the title bar, and select Properties.
- Go to the Font tab and make sure you select a UTF-8-compatible font like Consolas or Lucida Console (some older fonts don't support proper ASCII/UTF-8 rendering).
- Next, open Windows Settings > Time & Language > Language > Administrative language settings.
- Under the Administrative tab, click Change system locale..., then check the box for "Beta: Use Unicode UTF-8 for worldwide language support" (note: this might affect some older non-UTF-8 apps, but it's safe for most modern software).
- Restart your computer for the changes to take effect.
3. Verify Visual Studio Project Settings
Double-check these project settings to ensure alignment:
- Right-click your project in Solution Explorer → Properties.
- Go to Configuration Properties > General and set "Character Set" to Use Multi-Byte Character Set (or if you prefer Unicode, ensure it's set to Use Unicode Character Set—either works as long as your console encoding matches).
- Confirm your source file is still saved with Unicode (UTF-8 without signature) - Codepage 65001 via File > Save As > Save with Encoding.
After trying these steps, your printf statement should output exactly what you want: Use \n to cause a newline; use \t to cause a tab.
内容的提问来源于stack exchange,提问作者S. Kwon

