DEV Community

Cover image for Run C Code in Visual Studio Code
Haytham Mostafa
Haytham Mostafa

Posted on • Updated on

Run C Code in Visual Studio Code

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 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

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

Download VS Code - 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

2) Select Next

Step_2

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

4) Select Install.

Step_4

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

Step_5

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

Step_6

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
Enter fullscreen mode Exit fullscreen mode

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

3) MinGW software has been successfully downloaded.

4) Run mingw-get-setup file.

5) Click on the Install

MinGW_step-1

6) Set it defaults, click on the Continue

MinGW_step-2

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

MinGW_step-3

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

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

MinGW_step-5

10) Applying the MinGW packages

MinGW_step-6

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

MinGW_step-7

12) Set the Environment Path for the MinGW SetUp:

a. Copy the directory path, as shown below.
MinGW_ENV-1

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

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

gcc --version
g++ --version
gdb --version
Enter fullscreen mode Exit fullscreen mode

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/>.
Enter fullscreen mode Exit fullscreen mode

iii. Prepare VS Code for C Code

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

Extensions_Tab

2) Install C/C++ Extensions

Extensions

3) Install C/C++ Extensions Pack

Extensions_Pack

4) Install Code Runner Extension

Code_Runner

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

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

Add_Folder

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

Hello_C-2

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

Hello_C-3

5) Run code

Hello_C-4

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

Top comments (0)