Transcript for:
Getting Started with C in VS Code

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 uh just create a file by clicking on this new file button and then i'm going to name my file as main.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 where visual studio code is going to detect that you want to create a c a 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 uh 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 that 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++ extension from Microsoft which is the developer for Visual Studio code is going to run your C or C++ program is going to help you to debug or browse your C or C++ program and it's going to also give the IntelliSense capability in your C or C++ 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++ themes here. So it's 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 extension so i can just click on read load and it's going to 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's going to just add these two numbers and show us the result. 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. You can also create a build task here. So you can see there is this option called run build task. So I'm going to click on this run build. task and it's going to give me this suggestion which build task i want to create here it says gcc.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'm going to just click on this which is going to uh 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.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 main2.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.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.exe 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 asking me to enter the first number and the second number so I'm going to enter two as the first number five 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.c is the file where you write your program and main.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 breakpoint 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 breakpoints 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 MinGW, which comes with GDB debugger. So you can click on C++ GDB. And once you click on C++ 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.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 a garbage value because we haven't assigned any value to it. If we have assigned for example num is equal to 0 then it will show the 0 value. If you don't initialize your variables it's going to take some garbage value initially then I can click on step over which is going to go to this code where I will be asked to provide a number I can provide a number here for example and then my program execution stop to the second breakpoint 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 num1 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.