Debugging C Program with Visual Studio Code (VSCode)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
now inside the c apps let me create a very simple C file so I'm going to click on this new file button I can even click on file and then click on new file and from here I can create a new file right so let me just create a file by clicking on this new file button and then I'm going to name my file as main dot C and then press enter now as soon as I just create a main.c file you are going to see some kind of notification pair Visual Studio code is going to detect that you want to create a CR program and it's going to suggest you to install the related C or C plus plus extensions if it doesn't show any suggestions you can always go to the extension tab so just click on this extensions Tab and then search for C and C plus plus okay so C forward slash C plus plus and once you search for C and C plus plus the top ah extension here is from Microsoft you can see you can just select here this extension and then in your case you are going to see the install option I have already installed this extension in your case you are going to see the install button here so you just need to click on install so this C and C plus plus extension from Microsoft which is the developer for visual studio code is going to run your C or C plus plus program is going to help you to ah debug or browse your C or C plus plus program and it is going to also give the intellisense capability in your C or C plus plus program so just install this and once you have installed this all the features of this extension will be available on your Visual Studio code editor you can also see you have other extensions like C or C plus plus themes here so it is going to look like this so whatever extensions you can see are related to C or C plus plus I will suggest you to install all of them they are going to help you to better run your C or C plus plus programs on Windows operating system in Visual Studio code right now you can see reload is required for this extra extension so I can just click on read load and it's going to uh reload my visual studio code so now let me just go to this Explorer section here where I have created the main.c file I can write a very simple C program here which is going to ask for two numbers and it is going to just add these two numbers and show us the result right this is a very simple program you can find anywhere on the internet now in order to run this program what you can do you can either click on this terminal and open a new terminal and using GCC you can run your C program which is by just using the normal terminal command right you can also create a build task here so you can see there is this ah option called run build task so I'm going to click on this run build task and it's going to give me the suggestion which build task I want to create here it says GCC dot exe because I have created the C file and visual studio code is going to recognize that this is a C file and that's why it's giving us this gcc.exe option so I am going to just click on this which is going to start building my program you can see starting build and you can see the build is successful so my C program has been compiled successfully and you can see main.exe file has been created once the build is complete you can even run this GCC command in your terminal by opening a new terminal so you can click on this plus sign and it's going to open the default command prompt in my case it's Powershell you can even open the command prompt from here so you can see this is the command prompt so you can either use Powershell or command prompt to compile your program also right I have used this inbuilt capability of Visual Studio code to build my program let me show you how you can build the program using the command line also so here I can write GCC dot exe and then I can give the name of my main.c file so let me give the name of my file and then hyphen o and then the name of my output file which will be the executable file so I can name it as main two dot exe file for example right main exe file is already created by this GCC build option so I can create a second exe file and I can run this and you can see this main2.gcc file is created now in order to run these exe files these exe files are the output of your compilation of your C program right so when you want to run your C program you just need to give this command so whatever is the name of your exe file you can give the name of your exe file for example main dot exe file right so I want to run this main.exe file I can just type main.exe and in Powershell it's going to auto complete so I can just type main dot e x c here and then when I press tab it's going to just give dot backslash automatically to your file and then when you press enter it's going to ask me for the first number because my program is asking me to enter the first number and the second number so I am going to enter 2 as the first number 5 as the second number and it's going to show me the sum of these two numbers this is what this program is doing right in command prompt you can just write main dot EXE and then press enter and once again I am going to give the first number and then the second number and it's going to give the sum of these two numbers so main dot C is the file where you write your program and Main dot exe or main2.exe is the output of your program which you can run on the terminal using visual studio code now let's say I want to debug my program how can I do this so I can put a debugger option here so this is called the break point so I can go to any line and I can put the breakpoint on each and every line and when I go to debug option so when I click on debug option I will be able to see all my break points here right so I can see my breakpoint on line 6 and line 9 I can even put the breakpoint on line 12 and it will be added here you can toggle all the breakpoint which means they will be disabled and once you once again toggle they will be enabled you can just remove all the breakpoint using these options so this is how you can add or toggle or remove your breakpoints in order to run your program in debug mode you can click on run and debug and once you click on run and debug you can select your debugger in our case we have installed Min GW which comes with GDB debugger so you can click on C plus plus GDB and once you click on C plus plus GDB you will be able to see this gcc.exe option so I'm going to click on this gcc.exe option and once I click on GCC dot exe option my program is going to run and you can see the debugger has been started and you can see uh when the debugger starts you will be able to see all these options here so you can see at the top this run or continue option step over option then step into option and then step out option so you can see I have created my first breakpoint on line six so the program execution stopped on this line I can now see all the variables or local variables here so you can see in num1 there is a value of 16 which is the garbage value because we haven't assigned any value ah to S if we have assigned for example num is equal to 0 then it will show the zero value if you don't initialize your variables it is going to take some garbage value initially then I can click on step over which is going to go to this ah code where I will be asked to provide a number I can provide a number here for example and then my program execution stopped to the second break point which is this one I can step over I can provide a second number and then press on continue and then my program execution stops at sum is equal to num 1 plus num2 so I can just step over once again to the next line and you can see some value is 17 which is the sum of 8 and 9 and it's going to print this text when I once I click on continue here you can see the output is printed here so this is how you can use the debugger for the C program on Visual Studio code
Info
Channel: ProgrammingKnowledge
Views: 76,926
Rating: undefined out of 5
Keywords:
Id: NJYcRcqPyOw
Channel Id: undefined
Length: 11min 18sec (678 seconds)
Published: Sun Apr 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.