SOFTADDA

softadda.com

Visual studio code is an open source code editor developed by Microsoft for Windows, Linux and MacOS platforms. It is a software editor that has a rich extension of various languages like C++, C, Java, Python, GO, PHP etc. In Visual studio code, we can change the background theme, keyboard shortcuts on our preferences, install an extension an add additional functionality.

Prerequisites for running a C Program in VsCode.
  1.  Visual studio code editor must be installed in the system. Want to know how to download Vs Code? Click here
  2.  Download the C/C++ extension.
  3. Download the C/C++ compiler. There are some popular compiler like:-
    • GCC on Linux.
    • GCC via MinGW-w64 on windows.
    • Microsoft C++ compiler on windows.
    • Clang for XCode on MacOS.

We have already installed the Vs code in our system. Its look like as shown in image given below.

image2
1. Download & install the C/C++ extension.
c programming
  • Click on the extension button that display on sidebar for downloading and installing the extension.
  • Search C/C++ extension in the search box .
  • After that, Click on the C/C++ extension. Then click on the install button to install the C/C++ extension.
  • After installing the C/C++ extension pack of 9, its look like as shown in image given below.
image4
  • Now we have to install another extension called Code Runner.
image5
2. Download MinGW
  • Get the latest version of MinGW-w64 via MSYS2, which provides up-to-date native builds of GCC, MinGW-w64 and other helpful C++ tools and libraries.
  • You can download the latest installer from the MSYS2 page or use this link to the installer.
  • Follow the installation instructions on the MSYS2 website to install MinGW-w64.
3. Setting Environment variable
  • Add  the path to your MinGW-w64 bin folder to the windows path environment variable by using the following steps:-
    1.  In the windows search bar, type and search for Edit environment variables for your account.
    2.  Choose the path variable in your User variable and then select Edit.
    3.  Select New and add the MinGW-w64 destination folder path to the system path. For example:-  C:\msys64\mingw64\bin.
    4.  Select Ok to save the updated Path.

Checking MinGW installed or not

To check that your MinGW-w64 tools are correctly installed and available, open a new command prompt.

  •  Type:  g++  – -version and click Enter
  • Again type:  gdb  – -version and click Enter.

If you don’t see the expected output or g++ and gdb is not recognized command, make sure your path entry matches the Mingw-w64 path location where the compilers are located. If the compilers do not exist at that path entry, make sure you followed the instructions on the MSYS2 website to install Mingw-w64. 

4. Writing first program in C
  • Open visual studio code in your system.
  • Make a file with extension  .C “.
  • Now lets type your first code. You can copy and paste the code that I have provided below.
  • Click on the run button or press keyboard shortcut  button “Ctrl+Alt+N” .
				
					//Simple C Program to display "Hello world".
//Header file for input and output function.
#include<stdio.h>
    
//main function:-
//where the execution of program begins.
int main()
{

//Prints hello world.
printf("Hello World");

return 0;
}
				
			

output:

Hello World

  • Here Output tab is read only. Hence we cannot take any input from the user.
  • Following steps to take input from the user:- 
    1. Click on manage button at the bottom of the visual studio code, and select settings.

 

2.  After clicking on settings, select the extension button to set the setting for the C compiler. 

3.  Scroll the drop-down box to select the Run Code configuration.

4.  Now scroll the right-side pane and tick on the Run in Terminal.

I hope this blog was helpful, If you are facing any problem while installing let me know in the comment section.