How to Run C in Visual Studio Code on Windows 11

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this video I'm going to show you how you can set up C support on your Visual Studio code editor on your windows 11 operating system so for this you will need two things first one is obvious which is Visual Studio code so if you haven't installed Visual Studio code I will recommend you to install it it's a very easy and simple process and if you don't know how to install Visual Studio code I will put the link of the video in which I have shown how you can install and use Visual Studio code and once you have Visual Studio code we need a second tool which is called a compiler right so a compiler will help you to compile your CNC plus code on any operating system so for Windows we are going to install the Min GW compiler so let's start with that so first of all open your favorite browser and search for bin GW and in the search result you will see some websites so don't click on the first website which you will see here which is from Source Forge you need to open this website which says Min GW hyphen w64.org so just click on this link which says Min GW hyphen w64.org and once this website opens you will be able to see the overview here and on this website you can click on the downloads link here so here you will be able to see all the download link for bin GW for different operating systems so the one we are looking for is the windows version so we are going to click on this link which says Min GW w64 builds so just click on this link which is going to open this kind of window again and from here you can download the Min GW builds from Source Force so you need to click on this link now right so Source forged link was shown directly in the search result but you need to go through this website in order to come to this website and once you are redirected to the sourceforge.net website you will see that Min GW 64-bit installer exe file will be downloaded on your windows 11 operating system so once this file is downloaded I'm going to click on this file and then you will see this warning which says do you want to allow this app from unknown publisher to make changes on your device I trust this so I will click on yes here and I'm going to minimize this browser and you can see Min GW installer has been started on my windows 11 operating system so on the first window I just need to click on next here which is going to start the installation here you can choose the version for Min GW so I'm going to choose the top most option which is 8.1.0 at the time of making this video you can also choose the architecture I'm going to choose x8664 and then you can choose the threads so generally for Threads I always choose posix but you can also choose win32 also also exception you have two options I will leave it as default and then you have the build revision I will leave it as default also and then click on next and this will be the location where your Min GW compiler will be installed so you can see it will be installed in the program files Min GW directory will be created and then this folder will be created right so if you don't have the good reason to change this location just leave it as default and also you can leave this checkbox as checked which is going to create a shortcut for your Min GW compiler tool so I'm going to click on next now which is going to start the download of all the files and packages for your mingw tools so just wait for this process to reach to the 100 Mark so now after some time I can see this window which says click next to continue the installation so I'm going to click on the next and now I can see this message which says Min GW has been successfully installed and I can click on finish to complete the installation so let me click on the Finish button so once I click on the Finish button Min GW is installed on my Windows operating systems so now once mingw is installed first of all we need to know the location where Min GW is installed so at the time of installation you might have noted the location so let me show you the default location where Min GW will be installed so you need to go inside the C directory and then go inside program files and then you will be able to see this Min GW hyphen W 64 bit folder here and then this folder will be there so this is the location where Min GW is installed then you need to go inside Min gw64 and here under the bin folder you will see all the files which will help you to compile your C or C plus plus programs okay so go until the bin folder right and you need to copy this path now we need to set the environment variable for Min GW so that we can use GCC or G plus plus command on our Windows command prompt or Powershell or terminal so for that what you need to do you just need to click on search and then search for edit EnV and it's going to show you this result which says edit environment variables for your account and you can see this window is going to open right now if you can't find this edit environment variable window using the search option then you can always open the control panel and then once control panel opens you need to view by category here and then click on system and security and then once again click on system here which is going to open the settings app and here it's going to show you an about section this link which says Advanced system settings so just click on that and then click on environment variables which is also going to open the same window so you can choose any one of the method so once this environment variables window opens select path under system variables and then click on edit and then click on new here okay so it's going to allow you to add a new environment variable and then just copy the path until the bin folder inside your Min gw64 bit folder okay so this path is until bin folder okay and once you copied it you can just add this path here inside your environment variables and then just press enter which is going to add your path and then click OK here and then click OK and then once again click on OK and now you can close all the windows here and now we can test our GCC and G plus plus versions and commands right so you can open the command prompt by just pressing CMD and then open the command prompt from the result and once the command prompt is open here you can just write GCC space hyphen hyphen version and then press enter and if you see some result which looks like this that means GCC command is working successfully on your windows 11 operating system similarly you can also check the G plus plus command which is also used to compile your C plus plus programs so it's also going to show you this kind of result here so once you see this kind of result that means your GCC and G plus plus commands are working so once you have bin GW compiler and visual studio code on your Windows operating system let's create a c program and let's see how we can set up a c program with Visual Studio code so what I'm going to do is I'm going to create a folder on my desktop you can create a folder at any location on your Windows operating system so that let me right click here and then let me just select new and then create a new folder I'm going to name my folder as C apps and then press enter and then I'm going to go inside this c apps folder then I can right click on this folder and then I can click on show more option and I can see this open with code option so I can just click on open with code which is going to open Visual Studio code in this folder okay so you can see see apps is open right here you can just click on I trust the author and you can see the c apps folder has been opened on Visual Studio code right you can do that or if you are a fan of command line you can open the command line so right click here and then click on open in terminal and once the terminal opens you can even open the visual studio code in this c apps folder by just writing code space dot this means I want to open the visual studio code in the current directory so I am going to press enter which is also going to open Visual Studio code in the current directory in which I am present so right now I am present in the c apps folder and that is what it's going to open in Visual Studio 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 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 is 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 ok 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 COC 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'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 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 dot 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 2 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 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 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 so this is how you can set up the C programming setup on your Visual Studio code I hope you enjoyed this video and I will see you in the next video
Info
Channel: ProgrammingKnowledge
Views: 76,576
Rating: undefined out of 5
Keywords: visual studio code download, cl.exe build and debug is only usable when vs code is run from the developer command prompt for vs., how to compile c++ in visual studio code, microsoft visual c++ (msvc) compiler toolset, visual studio code c++, mingw-w64, how to run c program in visual studio code, c programming in vs code
Id: 4-_3obNgNKM
Channel Id: undefined
Length: 21min 55sec (1315 seconds)
Published: Tue Apr 11 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.