https://code.visualstudio.com/docs/languages/cpp

Some time ago a friend of mine asked me lớn recommend her an IDE lớn compile and run C và C++ code because she was tired of using XCode, and I don’t blame her, Xcode is like killing a fly with a cannon if you only need to vày simple things.

Bạn đang xem: How to run a c program in visual studio code

As a regular user of VSC, that was the first thing it came lớn my mind, but then I remembered that VSC is not an IDE per se, but a Code Editor that uses extensions khổng lồ behave like an IDE, I also remembered that I had a lot of time without coding on those languages và when I did, I used notepad++ & the terminal.

My objective then, và the topic of this post, discover how lớn configure VSC lớn behave lượt thích a C/C++ IDE.

Disclaimer: This post is based on macOS, but since VSC is available in others OSs it shouldn’t be much different the steps here mentioned.

First of all, we need to tải về and install Visual Studio Code of course.

How I said at the beginning of the post, VSC uses extensions lớn behave like an IDE, so the next step is to install the extensions that will help us with our purpose.

Xem thêm: Thực Hiện Siêu Âm Mốc 32 Tuần, Mẹ Cần Chú Ý Gì? Siêu Âm Tuần 32

The first useful extension we are gonna need is called C/C++, this extension will help us with the Intellisense like code completion, parameter info, quick info, etc. We can install it from here, when clicking install, it will open VSC & install it automatically. Or we can xuất hiện VSC, go to lớn Code > Preferences > Extensions (also using the shortcut shift + cmd + X) type C++ và press install.


*

*

code runner

What this extension does is lớn run a terminal command using a compiler installed in our computer, in the case of macOS uses gcc which is already installed, but in case you don’t have it you will need khổng lồ install the Command Line Tools for Xcode.

After we have installed these two extensions we are ready khổng lồ give it a try. Let’s write a simple Hello World.

#include using namespace std;int main() cout return 0;To run this code, we have three options:

Using cmd+alt+n.Using fn+F1 and type run code.Right-click on the editor and select Run Code.
*

*

OUTPUT Tab

You can see in the image above the exact command is being executed in order khổng lồ compile and run our code.

Until here everything seems fine, but since the OUTPUT tab is read-only, we cannot interact with our code if we need to. Khổng lồ be able to vì chưng so, we need to lớn tell the extension Code Runner lớn run our program in the TERMINAL instead of the OUTPUT tab following the next steps:

Go khổng lồ the menu Code > Preferences > Settings.In the User tab on the left panel, expand the Extensions section.Find and select Run Code Configuration.Find and kiểm tra the box Run in Terminal.
*

User Settings