大多数人在Windows上使用Visual Studio
IDE作为C语言学习环境,实际上我认为这是非常错误的做法。每个人都有各自的理由,比如学校是这么教的,要保持和课堂同步,又比如要做毕设等等,还有貌似合理的更高级说法,比如有人喜欢说工具是其次的,只要学好了C语言语法就行,其他任何环境也能快速上手。对不起,如果是针对一门跨平台的高级语言,这个说法成立,但是对于C语言,不成立。使用IDE会最大程度的弱化你对C语言整体的理解程度。
在这里,我提出几个比较通用C语言学习观点 1、C语言语法简单,真正学会C语言的关键不在于语法。 2、C语言不适合零基础学习,应当具备其他高级编程语言的基础再来学习才能事半功倍,有所体悟。 3、在Windows操作系统上是学不好C语言的。从C语言历史上看,C语言原本就是被发明在Unix操作系统上的,回到它真正的语境下才更有助于深刻理解。
最后,本教程针对的是已有其他编程基础的朋友,这也符号以上观点的第二条,因此不适合零基础学习。
基于以上观点,我们不应该选择Windows和IDE作为学习C语言的环境,最好使用Linux系统来学习,推荐Ubuntu操作系统。当然,使用Linux系统本身也需要学习很多知识,不适合小白用户,而且纠结于Linux命令的用法,也脱离了学习C语言的初衷,无端增加了学习障碍,得不偿失。
因此,我们采用一种折中的更灵活的方案,在Windows上使用GCC编译器以及一些GNU工具链来模拟Linux 下的C语言学习体验,这样一来,我们既能使用熟悉的Windows系统,也能利用GNU工具链来学习C语言,熟悉了GNU工具链,就相当于掌握了Linux C的开发环境。
下载一个64位Windows版本的GCC——MinGW-W64
[下载地址] https://sourceforge.net/projects/mingw-w64/files/
预览图
如上,它有两个版本,sjlj
和seh
后缀表示异常处理模式,seh
性能较好,但不支持 32位。 sjlj
稳定性好,可支持 32位,推荐下载seh
版本
将编译器安装到自己准备好的目录,完成安装后,还需要配置一下环境变量,将安装目录下的bin
目录加入到系统Path环境变量中,bin
目录下包含gcc.exe
、make.exe
等工具链,这些也就是GNU工具链。(相信有过编程经验的人,大都知道环境变量怎么配置,如不知道,请直接去Google搜索 Windows系统如何配置环境变量)
检查环境
配置完成后,检测一下环境是否搭建成功,打开cmd
命令行,输入gcc -v
、make -v
能查看版本号则成功。
能同时满足简单好用、界面美观、功能强大、轻量级等特点的编辑器并不多,这里推荐最目前最流行人气最高的VS Code编辑器。
首先下载VSCode
编辑器 ,然后安装插件并配置调试环境。官方下载地址 https://code.visualstudio.com/
注意,这里是VSCode
编辑器,不是Visual Studio
IDE,这完全是两个不同的东西,虽然都是微软出品。
有了编辑器,但我们还需要安装一个C/C++
插件,它能进行一些代码智能提示、语法检查之类的辅助功能。我们打开编辑器,在插件商店中搜索C/C++插件,点击Install
进行安装,安装完成后,可能需要重启编辑器。
在这里插入图片描述
实际上不配置调试环境一点也不影响我们写代码,但是在学习C语言的过程中,借助调试功能可以帮助我们形象的理解很多概念,而且调试功能对于我们查找BUG等问题非常有帮助。
如何配置调试环境呢?实际上VS Code编辑器并不具备任何调试功能,调试功能是GNU工具链提供的,它在我们之前安装MinGW-W64的那个bin
文件夹里面,它的名字叫gdb.exe
,如果大家之前的环境配置Ok,那么可以在cmd命令行输入gdb -v
查看调试器的版本。这其实就是告诉大家,即使你不配置VS Code,也可以直接在命令行输入gdb
命令来调试代码,但是这样会显得繁琐和不友好,配置一下VS Code就能通过图形界面的方式来调试程序,体验瞬间提升。
这里给出一个官方提供的配置调试环境的完整指导教程,如果英语好,有耐性还是建议好好看看 [官方配置指南] https://code.visualstudio.com/docs/cpp/config-mingw
具体步骤如下
c_code
,然后新建一个文本文档,改名为test.c
文件c_code
文件夹test.c
文件打开,并输入如下测试代码保存 1 #include<stdio.h>
2
3 int main(){
4 int i,j,k;
5
6 i = 10,
7 j=8,
8 k=2;
9 printf("result is %d",i+j+k);
10 }
4 . 按【F5】键调试,这时会弹出一个对话框,选择打开编辑
launch.json
配置文件,有时候可能会直接跳出launch.json的配置文件,根据实际情况修改默认配置即可
1 {
2 "version": "0.2.0",
3 "configurations": [
4 {
5 "name": "(gdb) Launch",
6 "type": "cppdbg",
7 "request": "launch",
8 "program": "${workspaceRoot}/${fileBasenameNoExtension}.exe",
9 "args": [],
10 "stopAtEntry": false,
11 "cwd": "${workspaceFolder}",
12 "environment": [],
13 "externalConsole": true,
14 "MIMode": "gdb",
15 "miDebuggerPath": "G:\\developer\\mingw64\\MinGW\\bin\\gdb.exe",
16 "preLaunchTask": "build c project",
17 "setupCommands": [
18 {
19 "description": "Enable pretty-printing for gdb",
20 "text": "-enable-pretty-printing",
21 "ignoreFailures": true
22 }
23 ]
24 }
25 ]
26 }
有一个地方需要特别说明,请将如上配置文件中的"miDebuggerPath"
值配置成你自己电脑上的gdb.exe
路径,其他的直接参照我这份配置即可。
5. 配置调试前的编译任务。我们要想调试代码,首先需要将代码加特定参数编译成可执行程序,这一步就是配置编译任务的。在编辑器中,使用组合快捷键【Ctrl】+【Shift】+【p】打开命令面板,输入Tasks:
进行联想,选择如下的Default Build Task
在这里插入图片描述 选择如下项,创建一个tasks.json配置文件
在这里插入图片描述
选择Others模板
1{
2 "version": "2.0.0",
3 "tasks": [
4 {
5 "label": "build c project",
6 "type": "shell",
7 "command": "gcc",
8 "args": [
9 "-g",
10 "¨E123Efile¨E125E""¨E45Eo""{file}",
11 "-o",
12 "<span class="katex-html" aria-hidden="true" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">¨<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E123<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E<span class="mord mathit" style="margin-right:0.10764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">f<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">i<span class="mord mathit" style="margin-right:0.01968em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">l<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">e¨<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E125<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E"<span class="mspace" style="margin-right:0.16666666666666666em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">"¨<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E45<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">o"<span class="mspace" style="margin-right:0.16666666666666666em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">"{fileBasenameNoExtension}.exe"
13 ],
14 "group": {
15 "kind": "build",
16 "isDefault": true
17 }
18 }
19 ]
大家直接参照我这里的配置文件配置即可,有一点说明一下,"label": "build c project"这里是给这个任务起了一个名字,这个名字叫什么不重要,但是它直接对应的是launch.json中的"preLaunchTask": "build c project"这一行,因此两边名字一定要一致,如果你改了这个label名的话。
6. 检测调试环境。给代码加一个断点,例如鼠标单击第6行的行号之前区域
在这里插入图片描述
如图,这里就给第6行代码打了一个断点,编辑器里面会呈现出一个红色小点,启动调试时,程序就会在第6行卡住。我们直接按【F5】键开启调试,会发现代码卡在了第6行,该行会变色,观察断点处也有变化
在这里插入图片描述
此时代码已处于调试状态,将鼠标移动到断点那行之前的变量上,即可查看此刻变量的值是多少,这就是所谓的调试。要记住,只能查看断点之前的变量,因为断点之后的代码还没有被执行。
在这里插入图片描述
如上图,不仅可以用鼠标查看,编辑器左侧面板也会显示出当前已经执行过的代码中的变量情况。此时,如想继续往下一执行,直接按一次【F10】代码就会往下再运行一行。
在这里插入图片描述
除了快捷键,也可以使用鼠标点击悬浮菜单中红框的键,它对应【F10】,其他控制键,大家自行探究。
如果想直接运行写好的代码,可以使用鼠标右键菜单运行
在这里插入图片描述
编辑器会弹出底部面板,显示输出的结果,同时,在当前代码目录下还会生成一个同名可执行文件,本例则生成了test.exe