Getting Started with C

C is a powerful general-purpose programming language known for its efficiency and flexibility. It's commonly used in system programming, embedded systems, and creating high-performance applications.

To start coding in C, you need to install a C compiler on your device. However, if you want to get started quickly, you can use our free online C compiler that allows you to run code on your browser—no installation required.

Now, for those who prefer to use C on your computer, one of the most popular C compilers is GCC (GNU Compiler Collection), which is available for various platforms, including Linux, macOS, and Windows.

Follow this guide which will teach you to install C on Windows, macOS, and Linux (Ubuntu).


Install C on Windows

To install a C programming environment on Windows, you'll need two main components:

  • VS Code: A text editor to write your code,
  • MinGW: A compiler that turns your C code into an executable program.

Follow the steps below to install C on Windows:

  1. Install VS Code
  2. Download MinGW Compiler
  3. Run the installer
  4. Add MinGW to System PATH
  5. Install C/C++ Extension in VS Code

Here's a detailed explanation of each of the steps.

Step 1: Install VS Code

Go to the VS Code Official website and download the Windows installer. Once the download is complete, run the installer and follow the installation process.

Click Finish to complete the installation process.

Step 2: Download MinGW Compiler

Visit the official MinGW website and download the MinGW installation manager.

C MinGW Installation
C MinGW Installation

Step 3: Run the Installer

Now, go to your downloads folder and run the installer you just downloaded. You will be prompted to this screen.

C Run MinGW Installer
C Run MinGW Installer

Click on Continue and wait till the download is completed.

Include gcc-core package

During installation, you will be prompted to select the components to install. Mark mingw32-base for installation, click on installation and apply changes.

C GCC-Core Package Installation
C GCC-Core Package Installation

This will include the gcc-core package, which contains the GCC compiler for C.

Step 4: Add MinGW to System PATH

Go to the folder and double click on the MinGW folder and copy its location.

C:\MinGW\bin

Search environment variable on the terminal. In system properties, click on environment variables. You will be prompted to the screen below.

C Setting Environment Variables
C Setting Environment Variables

Then, find the Path variable in the System variables section and click on Edit. Click New and add the path to the bin directory within your MinGW installation (i.e. C:\MinGW\bin)

FInally, close all the dialogues with the Ok button.

Step 5: Install C/C++ Extension in VS Code

Open VS Code and click on Extensions in the left side of the window.

Then, search for C/C++ by Microsoft in the Extensions and click on install.

Installing C Extension in Windows
Installing C Extension in Windows

Now, you are all set to run C code inside your VS Code.

Note: Alternatively, you can use IDEs like Code::Blocks or Visual Studio, which come with their own C compilers.

and this is a code block

Install C on macOs

To install C on your Mac, you'll need two main components:

  • VS Code: A text editor to write your code,
  • Clang: A default compiler in macOS that turns your C code into an executable program.

Follow the steps below to install C on macOS,

  1. Install VS Code
  2. Install C Extension
  3. Check for C Compiler

Here is a detailed explanation of each of the steps:

Step 1: Install VS Code

Go to the VS Code official website and download the zipped file. Once the download is complete, open the zipped file.

In Finder, open a new window and navigate to the Applications folder. To install the VS Code application from the zip file, drag it into the Applications folder.

You can now launch VS Code directly from the Applications folder.

Step 2: Install C Extension

Open VS Code and click on the Extension in the right side of the screen. Search for C/C++ by Microsoft in the Extensions and click on install.

Installing C Extension in macOS
Installing C Extension in macOS

Step 3: Check for C Compiler

The macOS generally comes with Clang installed on it, which can be used as a C compiler. You can verify its installation by typing the following command in the terminal.

clang --version
Checking Clang Version
Checking Clang Version

If the output on your screen does not match the screenshot above showing the version of Clang, you may need to install Xcode Command Line Tools. You can do this by running the following command in your terminal

xcode-select --install

Now, you are all set to run C code inside your VS Code.

Install C on Linux

Linux has various distributions, and the installation process differs slightly from each other. For now, we will focus on Ubuntu distribution.

To run a C program in Linux, we need two main components:

  • VS Code: A text editor to write your code.
  • GCC: A compiler that turns your C code into an executable program.

Follow the steps below to install C on your Ubuntu system:

  1. Install VS Code
  2. Install GCC

Step 1: Install VS Code

Open the Terminal and type

sudo apt update  

This ensures the software packages are up to date.

Proceed to install VS Code with

sudo snap install code --classic 
Install VS Code in Linux
Install VS Code in Linux

Step 2: Install GCC

Install GCC and other essential build tools with

sudo apt install build-essential 
Install GCC in Linux
Install GCC in Linux

This package includes GCC and the utilities needed for compiling software.


Run Your First C Program

First open VS Code, click on the File in the top menu and then select New File.

Create a New File in VS Code
Create a New File in VS Code

Then, save this file with a .c extension by clicking on File again, then Save As, and type your filename ending in .c. (Here, we are saving it as hello.c)

Now, write the following code into your file:

#include<stdio.h>
int main(){
    printf("Hello World");
    return 0;
}

Then click on the run button on the top right side of your screen.

C Run Program
C Run Program

You should see Hello World printed to the command prompt.

For Linux System

Instead of the run button as in Windows and macOS, you should follow the following steps to run your C program on Linux.

In Terminal, navigate to your file's directory, and compile with

gcc hello.c -o hello

After compiling successfully, you'll see a new file named hello (or hello.exe on Windows) in the same directory.

Run your program using

./hello 

Congratulations! You've written and executed your first C program.

Now that you have set everything up to run C programs on your computer, you'll be learning how the basic program works in C in the next tutorial.

Did you find this article helpful?