写下本文的目的是为了记录重装系统后在配置VScode的过程中遇到的困难

1.VScode与Clion抉择

1.1 为什么不选用Clion?

Windows的控制台默认编码为GBK,我想要UTF-8的编码,出现冲突,找了网络上的解决办法,第一种是将文件编码转为GBK即可解决,第二种是修改注册表取消勾选run.processes.with.pty,这种方法有个隐患,目前截止到2025年6月26日仍然存在该隐患。

在Clion中运行该代码

#include <stdio.h>
int main()
{
    int myvariable;
    printf("Enter a number:");
    scanf("%d", &myvariable);
    printf("%d", myvariable);
    return 0;
}

期望得到的结果

Expected output:

Enter a number:1
1

实际得到的结果

1
Enter a number:1

出处: https://stackoverflow.com/questions/16877264/c-c-printf-before-scanf-issue

1.2 选取VScode的原因

2.VScode配置C语言

VScode只是一个高级的文本编辑器,并不具有编译功能,需要额外安装编译器。MinGW-w64 - for 32 and 64 bit Windows Files : https://sourceforge.net/projects/mingw-w64/ 打开网址,下载文件在 File - Toolchains targetting Win64 - Personal Builds - mingw-builds - 8.1.0 - threads-posix - seh - x86_64-8.1.0-release-posix-seh-rt_v6-rev0.7z,下载解压x86_64-8.1.0-release-posix-seh-rt_v6-rev0.7z。

我选择将解压后的mingw64文件夹放置C盘,记录此文件夹的路径,最后使用win+s快捷键,搜索编辑系统环境变量,进入编辑系统环境变量,点击环境变量,找到系统变量 - Path - 编辑 - 新建 - C:\Users\Ayazuki\mingw64\bin ,一路点击确定。最后win+r,输入cmd,输入gcc -v,当得到gcc的版本号,说明编译器安装阶段完成。

接下来主要记录在UTF-8编码的文件遇到Windows控制台编码GBK中出现的中文乱码问题。

永久修改VScode控制台的编码为UTF-8而不改变本身Windows控制台GBK编码的格式,主要修改setting.json文件,在设置中搜索setting,找到编辑setting.json

{
    "workbench.colorTheme": "GitHub Dark",
    "git.autofetch": true,
    "explorer.confirmDragAndDrop": false,
    "editor.fontFamily": "Maple Mono Normal NL,Consolas, 'Courier New', monospace",
    "editor.minimap.enabled": false,
    "workbench.startupEditor": "none",
    "editor.fontSize": 16,
    "files.autoGuessEncoding": true,
    "terminal.integrated.profiles.windows": {
        "PowerShell": {
            "source": "PowerShell",
            "icon": "terminal-powershell",
            "args": [ 
                "-NoExit",
                "chcp 65001"
            ]
            //主要增加" args []这一部分内容"
        },
        "Command Prompt": {
            "path": [
                "${env:windir}\\Sysnative\\cmd.exe",
                "${env:windir}\\System32\\cmd.exe"
            ],
            "args": [ 
                "/k",
                "chcp 65001"
            ],
            //主要增加" args []这一部分内容"
            "icon": "terminal-cmd"
        },
        "Git Bash": {
            "source": "Git Bash"
        }
    },
    "settingsSync.ignoredSettings": [

    ],
    "workbench.editor.autoLockGroups": {
        "mainThreadWebview-markdown.preview": true
    },
    "explorer.confirmDelete": false,
    "workbench.settings.applyToAllProfiles": [

    ]
}

修改后ctrl+s保存,重启VScode,在控制台可以输入 chcp 验证,UTF-8的代码页为65001。

.c文件编译运行流程:

创建hello.c文件,Ctrl+shift+B 进行编译,然后./hello.exe运行即可

未来打算进行的工作

VScode与cmake相结合