1 乱码原因默认情况下Windows 控制台使用本地语言编码如中文系统的 GBK。C 语言源文件若以 UTF-8 编码保存程序输出的中文会以 UTF-8 编码传输到控制台。若控制台代码页与输出编码不匹配如 UTF-8 输出到 GBK 环境会导致乱码。2 解决方法在VScode里CtrlShiftP调出命令面板或者查看选项卡内选择命令面板搜索Preferences:Open Workspace Setting(JSON) 打开工作区设置即可打开setting.json文件随后在大括号内输入以下的内容。如果文件内已有其他内容在前文末尾加上逗号code-runner.executorMap: { javascript: node, php: C:\\php\\php.exe, python: python, perl: perl, ruby: C:\\Ruby23-x64\\bin\\ruby.exe, go: go run, html: \C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\, java: cd $dir javac $fileName java $fileNameWithoutExt, c: chcp 65001 cd $dir gcc $fileName -o $fileNameWithoutExt $dir$fileNameWithoutExt, cpp: chcp 65001 cd $dir g $fileName -o $fileNameWithoutExt $dir$fileNameWithoutExt }保存保存保存然后重新编译运行代码终端内的乱码即可恢复正常。如果还是不行在终端输入chcp 65001运行出现Active code page:65001之后就可以正常跑C了后续都不会乱码。但此后每一次打开都要重新输入一遍chcp 65001有点麻烦。可以继续在setting.json内加上以下代码即可实现自动激活UTF-8terminal.integrated.profiles.windows: { PowerShell: { source: PowerShell, args: [-NoExit, -Command, chcp 65001 $null] }, Command Prompt: { path: C:\\Windows\\System32\\cmd.exe, args: [/K, chcp 65001 nul] } }, terminal.integrated.defaultProfile.windows: Command Prompt本质就是给 VS Code 内置终端配置了启动脚本每次打开内置终端时自动先跑一遍chcp 65001不需要你再操作了。完整代码code-runner.executorMap: { javascript: node, php: C:\\php\\php.exe, python: python, perl: perl, ruby: C:\\Ruby23-x64\\bin\\ruby.exe, go: go run, html: \C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\, java: cd $dir javac $fileName java $fileNameWithoutExt, c: chcp 65001 cd $dir gcc $fileName -o $fileNameWithoutExt $dir$fileNameWithoutExt, cpp: chcp 65001 cd $dir g $fileName -o $fileNameWithoutExt $dir$fileNameWithoutExt }, terminal.integrated.profiles.windows: { PowerShell: { source: PowerShell, args: [-NoExit, -Command, chcp 65001 $null] }, Command Prompt: { path: C:\\Windows\\System32\\cmd.exe, args: [/K, chcp 65001 nul] } }, terminal.integrated.defaultProfile.windows: Command Prompt3.小总结chcp 65001将 Windows 控制台的代码页强制设置为 UTF-8代码页编号为 65001强制切换为 UTF-8 代码页后编码一致性问题被解决。此方法同样适用于 C、Python 等需输出多语言字符的场景原理一致。本方法只适用于VScode本身终端乱码问题。如使用外部终端则需修改系统设置本方法无效。