Setting Up Your Environment

You need two things to write C programs: a text editor (where you write your code) and a compiler (which turns your code into a program the computer can run). That’s it.

No expensive software. No subscriptions. Just free tools that have worked for decades and will keep working for decades more.

What’s a Compiler?

When you write C code, you’re writing instructions in a language humans can read. But your computer’s processor (CPU) doesn’t understand English or C - it only understands numbers. Really specific numbers called machine code.

A compiler is a program that translates your human-readable C code into the machine code your computer can actually run. It’s like a translator between you and the machine.

We’ll use a compiler called GCC (GNU Compiler Collection). It’s free, works on every operating system, and has been the standard since 1987.

Installing GCC

Pick your operating system:

Windows

We need to install GCC for Windows. Here’s how:

  1. Go to winlibs.com
  2. Download the latest release - look for UCRT runtime, Win64, and download the zip file
  3. Create a folder called C:\mingw64
  4. Open the zip file and copy everything inside it into C:\mingw64
  5. You should now have a file at C:\mingw64\bin\gcc.exe

Now we need to tell Windows where to find GCC:

  1. Press the Windows key + R to open the Run dialog
  2. Type sysdm.cpl and press Enter
  3. Click the “Advanced” tab
  4. Click “Environment Variables” at the bottom
  5. In the bottom section (“System variables”), find Path and click “Edit”
  6. Click “New”
  7. Type C:\mingw64\bin
  8. Click “OK” on all the dialogs to close them

Important: You need to open a new terminal window after this. The old ones won’t know about the change.

macOS

Apple includes the tools you need, but you have to ask for them. Open Terminal (search for it in Spotlight) and type:

xcode-select --install

A window will pop up. Click through to install the Xcode Command Line Tools. This includes a C compiler.

Linux

You probably already have GCC installed. If not, open a terminal and run one of these commands depending on your version of Linux:

# Ubuntu or Debian
sudo apt install build-essential

# Fedora
sudo dnf install gcc

# Arch
sudo pacman -S gcc

Check That It Worked

Open a terminal (Command Prompt or PowerShell on Windows, Terminal on macOS/Linux) and type:

gcc --version

You should see something like:

gcc (GCC) 13.2.0

The exact version number doesn’t matter. If you see a version number, you’re ready.

If you see “command not found”: Go back and check the installation steps. On Windows, make sure you opened a new terminal after changing the PATH.

The Text Editor: Visual Studio Code

For writing code, we’ll use Visual Studio Code (VS Code). It’s free, works on all operating systems, and makes writing C code easier with features like:

  • Colored syntax: Different parts of your code show in different colors, making it easier to read
  • Auto-complete: It suggests what you might want to type next
  • Built-in terminal: You can compile and run your code without leaving the editor

Installing VS Code

  1. Go to code.visualstudio.com
  2. Download and run the installer for your operating system
  3. Open VS Code
  4. Install the C/C++ extension:
    • Click the square icon on the left sidebar (Extensions)
    • Search for “C/C++”
    • Install the one made by Microsoft

Setting Up a Project Folder

  1. Create a folder somewhere on your computer for your C projects (like Documents/c-projects)
  2. In VS Code, go to File → Open Folder and select that folder
  3. Create a new file called hello.c

To open the built-in terminal: View → Terminal (or press Ctrl+` on your keyboard)

Your First Program

Type this into your hello.c file:

#include <stdio.h>

int main(void) {
    printf("Hello, World!\n");
    return 0;
}

Save the file (Ctrl+S or Cmd+S on Mac).

Compile and Run

In the terminal at the bottom of VS Code, type:

gcc hello.c -o hello

This tells GCC to:

  • Take your hello.c file
  • Compile it into a program
  • Name that program hello (the -o hello part)

Now run your program:

./hello

On Windows, type hello.exe instead.

You should see:

Hello, World!

Congratulations! You just wrote, compiled, and ran a C program.

Breaking Down the Code

Let’s understand what each line does:

#include <stdio.h>

This line says “include the standard input/output library.” This library contains the printf function we use to print text. Without this line, the compiler wouldn’t know what printf means.

int main(void) {

Every C program needs a function called main. This is where your program starts running. The word int means it will give back a number when it’s done. The word void means it doesn’t need any information to start.

    printf("Hello, World!\n");

This prints text to the screen. Whatever you put between the quotes gets printed. The \n at the end means “start a new line” - like pressing Enter.

    return 0;

This tells the computer “the program finished successfully.” Returning 0 means “everything went fine.” Other numbers usually mean something went wrong.

}

This closing brace marks the end of the main function.

What Happens When You Compile

When you ran gcc hello.c -o hello, a lot happened very quickly:

  1. Preprocessing: The #include line was replaced with the actual contents of stdio.h (thousands of lines of code that define printf and other functions)
  2. Compiling: Your C code was translated into a simpler language called assembly
  3. Assembling: The assembly was translated into the raw numbers (machine code) that your CPU understands
  4. Linking: The machine code for your program was combined with the machine code for printf (from the C library) into one final program

You don’t need to understand all of this yet. The important thing is: nothing is hidden from you. You could look at each step if you wanted to. C doesn’t hide what’s happening - that’s part of what makes it special.

Catching Mistakes Early

Try compiling with extra warnings turned on:

gcc -Wall -Wextra -o hello hello.c

The -Wall and -Wextra flags tell GCC to warn you about things that might be mistakes. These warnings can save you hours of debugging. Get in the habit of using them.

Try It Yourself

  1. Change the message to print your name instead of “Hello, World!”
  2. Add a second printf line to print something else
  3. Try removing the \n - what happens?
  4. What happens if you misspell printf as print?

Common Problems

“gcc: command not found” The compiler isn’t installed correctly, or your PATH isn’t set up right. Go back to the installation section for your operating system.

“hello.c: No such file or directory” You’re not in the right folder. Make sure your terminal is in the same folder as your hello.c file. You can use cd to change directories.

“undefined reference to main” You spelled main wrong, or you’re missing it entirely. Every C program needs a function called main (all lowercase).

Next Up

In Part 2, we’ll learn about variables - how to store and work with different kinds of data in your programs.


Enjoyed This?

If this helped something click, subscribe to my YouTube channel. More content like this, same approach - making things stick without insulting your intelligence. It’s free, it helps more people find this stuff, and it tells me what’s worth making more of.