Run C Code in Visual Studio Code
Tags: programming, vscode, beginners, coding
Published: 2023-05-03 00:23:59

#Why the C Programming Language Still Used?
C is a lot faster when compared to Python as it is designed to be a low-level language whereas in Python, the compiler first has to interpret the syntax before running the program. C++, built on C, is faster because of its object-oriented nature and is the most used language in competitive programming.

C is very close to hardware and can directly interact with it(managing memory). The C code you write is converted into assembly language by the compiler and then run as instructions on the hardware. This is helpful as you get to understand the working of compilers.

C is used in embedded hardware that forms a part of ovens, TV remotes, vending machines, IoT, etc. IoT has given rise to the idea of smart cities which I have explained in an earlier post.

C exists everywhere in the modern world. A lot of applications, including Microsoft Windows, run on C. Even Python, one of the most popular languages, was built on C. Modern applications add new features implemented using high-level languages, but a lot of their existing functionalities use C, such as:

• **Microsoft Windows**
Microsoft’s Windows kernel is developed mostly in C, with some parts in assembly language. For decades, the world’s most used operating system, with about 90 percent of the market share, has been powered by a kernel written in C.

• **Linux**
Linux is also written mostly in C, with some parts in assembly. About 97 percent of the world’s 500 most powerful supercomputers run the Linux kernel. It is also used in many personal computers.

• **Mac**
Mac computers are also powered by C, since the OS X kernel is written mostly in C. Every program and driver in a Mac, as in Windows and Linux computers, is running on a C-powered kernel.

• **Mobile**
iOS, Android and Windows Phone kernels are also written in C. They are just mobile adaptations of existing Mac OS, Linux and Windows kernels. So smartphones you use every day are running on a C kernel.

#Why we need to use Visual Studio Code?
[Visual Studio Code](https://code.visualstudio.com/) Also commonly referred to as VS Code, is a source-code editor made by Microsoft with the Electron Framework, for Windows, Linux and macOS. It's a very common and widely used text editor and IDE. Features include support for debugging, syntax highlighting, intelligent code completion, snippets, code refactoring, and embedded Git.

![VS Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7qk68nx3n1k2b6g0sd01.png)

##Prerequisites for run C in VS Code
**i. Download and install VS Code**

Make sure to download the exact file for your operating system.

![Builds](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5kh6w2qyi4i3otgxthwb.jpg)

[Download VS Code](https://code.visualstudio.com/download) - Quickly find the appropriate install for your platform (Windows, macOS and Linux)

The installation process is basic. Everything you will see in the following installation process it’s going to be the same for your VS Code installation as well.

1) Select the box "**I accept the agreement**" and click **Next**.

![Step_1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9kcz0udx4zxtzgz650vz.jpg)

2) Select **Next**

![Step_2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hu9e0d9m9efi6cl6l7k3.jpg)

3) Select the required options as per your need by clicking in the checkbox,
e.g.: **Create a desktop icon**

Then select **Next**.

![Step_3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wff3xfvmzvh0vei3066j.jpg)

4) Select **Install**.

![Step_4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/crty2xuljlrgghqbk9yy.jpg)

5) It might take a bit while to complete the installation.

![Step_5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/abjbhwgsy2ybiphl775n.jpg)

6) Click **Finish** (Check in the check box to launch VS Code).

![Step_6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9o8dmn9nvk79iiukhqch.jpg)


**ii. Download and Install Compiler Extension**

For running C or C++ code, you just need to have a valid C/C++ compiler installed on your computer. 

1) Check C and C++ compilers
Open your terminal and run:

```
gcc --version
g++ --version
gdb --version
```
If you get the version number, then the compiler is already installed on your system, otherwise you need to proceed with the next step. 

2) Download the MinGW-w64 Compiler
Go to the https://sourceforge.net/projects/mingw link

![source_forge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vkjv4g0bqet1etawu7cm.jpg)

3) MinGW software has been successfully downloaded.

4) Run mingw-get-setup file.

5) Click on the **Install**

![MinGW_step-1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/om67vqeqz2nhwqbn932a.jpg)

6) Set it defaults, click on the **Continue**

![MinGW_step-2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ixz14f4s2xi5fvl5zst.jpg)

7) After clicking the continue button, it shows step 2 of **MinGW** Installation Manager.

![MinGW_step-3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n7j08fzhtifcweylar7g.jpg)

8) As we click on the **Continue**, it shows the below image. In the **MinGW** Installation Manager, we need to check the **Mingw32-base** package, **Ming32-gcc-g++** package and **Ming32-gcc-objc** package to run and compile the C/ C++ program in the visual studio code.
After selecting the checkbox, click on the Installation tab (at the top left corner of the dialog box).

![MinGW_step-4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/glrb7fzl0jc3l6kphk1z.jpg)

9) Click on Apply Changes to set the package's installation in **MinGW**

![MinGW_step-5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t5flr5i6z3973qd5h8pb.jpg)

10) Applying the **MinGW** packages

![MinGW_step-6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cg7n8gax7d9qaelf9h9h.jpg)

11) Click on **Close** after all the changes have been successfully applied. 

![MinGW_step-7](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dbcesyx3q42zm7bowqss.jpg)

12) Set the Environment Path for the **MinGW** SetUp:

a. Copy the directory path, as shown below.
![MinGW_ENV-1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ajq67irmy6cy191zuf2.jpg)

b. In the **System Variables Path** click on the **Edit** button, then click on the **New** button and then paste the **C:\MinGW\bin** path; after that, click **OK** 

![MinGW_ENV-2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dlpxby7kcg04b51p67g5.jpg)

13) Check C and C++ compilers again now,
Open your terminal and run:

```
gcc --version
g++ --version
gdb --version
```
The following outputs are expected now:

```
**gcc --version**
gcc.exe (MinGW.org GCC-6.3.0-1) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

**g++ --version**
g++.exe (MinGW.org GCC-6.3.0-1) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

**gdb --version**
GNU gdb (GDB) 7.6.1
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "mingw32".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
```


**iii. Prepare VS Code for C Code**

1) Go to the Extension tab. Search for **"C/C++"**

![Extensions_Tab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yaw5d9rn9ayy6077vi7i.jpg)

2) Install **C/C++** Extensions

![Extensions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ppxg305xpk94igtx97p.jpg)

3) Install **C/C++** Extensions Pack

![Extensions_Pack](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8y1mjshxiwzux90wln6p.jpg)

4) Install **Code Runner** Extension

![Code_Runner](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/48hhn4uj3f70nx4qxswy.jpg)


**iv. Start C Coding**

1) Here we created a **Hello C** folder to store the program code. **Note:**You can create a folder with any name in any directory.

![Hello_C](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q8omsgibawubgphke0bm.jpg)

2) Go to the VS Code and **Add a Folder to a Workspace**.

![Add_Folder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u41kkw0gvuxxk7deyz2r.png)

3) After selecting the folder, you can add files as shows below:

![Hello_C-2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rdy3colazs9ha66zvw3b.jpg)

4) After creating the first file **hello.c**, write your first code

![Hello_C-3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/smbojkv4vdv8hgtzch75.jpg)

5) Run code

![Hello_C-4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gbz68m4ftiaxrayqf3wg.jpg)

**Conclusion**
Much appreciated for reading the full article, I hope it be useful.