Compile, run C/C++ code in visual studio

    In order to compile and run C/C++ code no need to install additional IDE.  If you are already using Visual studio code for any other purposes you can setup it with some extensions in order to easily compiler and run C code. 

In order to do that follow these steps:

Step 1:

Open Visual studio code and Press Ctrl + Shift + X which will open the extension pack manager in visual studio code.

Search and install the C/C++ extension


Step 2:

Open your existing C/C++ file in Visual studio code.


In the above image I created a folder named C projects and inside it I have a basic Hello World C program as shown.


Step 3:

Press Ctrl + Shift + B

The options would appear like this


Choose the first option which uses gcc compiler to compile the c program and hit enter.

Now you can see the compilation status in terminal which pops up automatically.  ie. If there are any errors in your source code then it would be displayed in the terminal.

If there are no errors and compilation succeeded then notice that there is a newfile named helloworld has been created in our project folder.


There is a difference between helloworld.c file and the helloworld file which has been automatically created when we compiled it.  helloworld is the compiled file so you can directly run it from your terminal.

Step 4:

Till now we have successfully compiled the code.

In order to run the file press Ctrl + J in Visual studio code to open terminal in the currently working project directory.

Run the following command:

./helloworld

In the above command ./ means open and the helloworld is the file name.

Now you can see the output as follows:


Bonus tip:

 use the up arrow key to use the previous command in your terminal.  Using this shortcut may save some time for you.

Comments