蓝色天空网mbsky.com · 知识 · 实践 · 创造C · 本地阅读预览
教程 / 编程语言 / C

C VS Code 开发环境

讲解使用 Visual Studio Code 搭配微软 C/C++ 扩展编写与编译 C 程序的完整步骤,完成 3 张已缓存界面截图核查,并在离线 GCC C11 容器实测 test.c 构建结果。

本站独立整理的内容草稿。实例显示的是预期结果;浏览器、服务端运行与技术内容验收尚待完成,本页只用于阅读和图文核对。

学完这一章,你能够

Visual Studio Code 与 C/C++ 扩展简介

Visual Studio Code(简称 VS Code)是微软开发的免费、跨平台轻量级源代码编辑器,支持 Windows、Linux 与 macOS。安装微软官方提供的 C/C++ 扩展后,只要打开或新建 `*.c` / `*.cpp` 源文件,编辑器便会自动启用语法着色、IntelliSense 智能补全、符号悬停文档与实时静态错误检查,显著提升 C 语言编码效率。

部署过程分为两步:首先访问官网 `code.visualstudio.com` 下载对应系统安装包,在 Windows 安装向导的“Select Additional Tasks”步骤中建议勾选桌面图标、右键“Open with Code”菜单、注册支持文件类型以及“Add to PATH”选项,确保命令行可直接使用 `code` 命令;其次启动 VS Code,点击左侧活动栏的扩展图标(或按 macOS 快捷键 `Shift+Command+X` / Windows 与 Linux 快捷键 `Ctrl+Shift+X`),在搜索栏输入 `C++` 并安装微软发布的官方 C/C++ 扩展。

新建 test.c 并在集成终端编译运行

完成扩展安装后,可在欢迎页点击“新建文件...”,在新建的空白标签页点击“选择语言”并在下拉搜索框中输入 `c`,随后将文件保存为 `test.c`。写入包含 `#include <stdio.h>`、`int main()` 与 `printf("Hello, World! \n");` 的代码后,右键文件在集成终端中打开所在目录,执行 `gcc test.c -o test && ./test`(Windows 为 `test.exe`)即可看到输出结果。本站已在离线 GCC 13.3.0 C11 容器下完成同类 `test.c` 的编译运行与源码读写实测。

实测 VS Code 章节示例 test.c 的 C11 输出、标准宏与源码持久化

c · 阅读源码
#include <stdio.h>
#include <string.h>

int main(void) {
    char out[64];
    int len = snprintf(out, sizeof(out), "Hello, World! \n");
    int c1_pass = (len == 15 && strcmp(out, "Hello, World! \n") == 0);

    long stdc_ver = __STDC_VERSION__;
    int ptr_bytes = (int)sizeof(void *);
    int c2_pass = (stdc_ver == 201112L && ptr_bytes == 8);

    const char *path = "/tmp/vscode_test.c";
    const char *src = "#include <stdio.h>\nint main(void) {\n    printf(\"Hello, World! \\n\");\n    return 0;\n}\n";
    FILE *fw = fopen(path, "wb");
    size_t wn = 0;
    if (fw) {
        wn = fwrite(src, 1, strlen(src), fw);
        fclose(fw);
    }
    char rbuf[256] = "";
    FILE *fr = fopen(path, "rb");
    size_t rn = 0;
    if (fr) {
        rn = fread(rbuf, 1, sizeof(rbuf) - 1, fr);
        rbuf[rn] = '\0';
        fclose(fr);
    }
    remove(path);
    int c3_pass = (wn == strlen(src) && rn == wn && strcmp(rbuf, src) == 0);

    printf("{\"runtime\": \"GCC C11 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [\n");
    printf("  {\"name\": \"test_c_hello_output\", \"expected\": {\"len\": 15, \"matched\": true}, \"actual\": {\"len\": %d, \"matched\": %s}, \"passed\": %s},\n",
           len, c1_pass ? "true" : "false", c1_pass ? "true" : "false");
    printf("  {\"name\": \"compiler_diagnostics_and_c11\", \"expected\": {\"stdc_version\": 201112, \"ptr_bytes\": 8}, \"actual\": {\"stdc_version\": %ld, \"ptr_bytes\": %d}, \"passed\": %s},\n",
           stdc_ver, ptr_bytes, c2_pass ? "true" : "false");
    printf("  {\"name\": \"terminal_build_file_check\", \"expected\": {\"bytes\": %zu, \"matched\": true}, \"actual\": {\"bytes\": %zu, \"matched\": %s}, \"passed\": %s}\n",
           strlen(src), rn, (strcmp(rbuf, src) == 0) ? "true" : "false", c3_pass ? "true" : "false");
    printf("]}\n");
    return 0;
}
预期结果 · 待实际运行核验
  • 确认输出精确匹配 "Hello, World! \n"(15 字节)、__STDC_VERSION__==201112L 且 64 位指针占 8 字节,临时 test.c 读写一致。
实测 VS Code 章节示例 test.c 的 C11 输出、标准宏与源码持久化

所需环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC 13.3.0 C11 linux/amd64,--network none)。

当前源码 SHA256:db4800b87e0f4e75a951f7959cdd16fc5caaa417282081fc9833bfe2fc654f43

下载本站示例源码

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载C VS Code 开发环境实验

所需环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC 13.3.0 C11 linux/amd64,--network none)。

ZIP SHA256:07986bbbda32c4cf92d339d1013ae68bb1761866d9b4660014edbbcea43dfb10

常见错误与边界

  • 实际端口、文档资源与应用配置都应和测试条件一致。

练习与解答

在 VS Code 中仅安装微软 C/C++ 扩展后,是否就能直接编译生成可执行文件?

展开参考解答

不能。C/C++ 扩展提供的是语法高亮、IntelliSense 补全与调试集成,仍需在系统中安装实际的 C 编译器(如 GCC、Clang 或 MSVC)并配置到 PATH 中才能完成编译链接。

规范与官方依据