C VS Code 开发环境
讲解使用 Visual Studio Code 搭配微软 C/C++ 扩展编写与编译 C 程序的完整步骤,完成 3 张已缓存界面截图核查,并在离线 GCC C11 容器实测 test.c 构建结果。
学完这一章,你能够
- 讲解使用 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 输出、标准宏与源码持久化
展开查看收起
#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;
}
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
不能。C/C++ 扩展提供的是语法高亮、IntelliSense 补全与调试集成,仍需在系统中安装实际的 C 编译器(如 GCC、Clang 或 MSVC)并配置到 PATH 中才能完成编译链接。