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:
- Go to winlibs.com
- Download the latest release - look for UCRT runtime, Win64, and download the zip file
- Create a folder called
C:\mingw64 - Open the zip file and copy everything inside it into
C:\mingw64 - You should now have a file at
C:\mingw64\bin\gcc.exe
Now we need to tell Windows where to find GCC:
- Press the
Windows key + Rto open the Run dialog - Type
sysdm.cpland press Enter - Click the “Advanced” tab
- Click “Environment Variables” at the bottom
- In the bottom section (“System variables”), find
Pathand click “Edit” - Click “New”
- Type
C:\mingw64\bin - 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 --installA 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 gccCheck That It Worked
Open a terminal (Command Prompt or PowerShell on Windows, Terminal on macOS/Linux) and type:
gcc --versionYou should see something like:
gcc (GCC) 13.2.0The 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
- Go to code.visualstudio.com
- Download and run the installer for your operating system
- Open VS Code
- 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
- Create a folder somewhere on your computer for your C projects (like
Documents/c-projects) - In VS Code, go to File → Open Folder and select that folder
- 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 helloThis tells GCC to:
- Take your
hello.cfile - Compile it into a program
- Name that program
hello(the-o hellopart)
Now run your program:
./helloOn 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:
- Preprocessing: The
#includeline was replaced with the actual contents of stdio.h (thousands of lines of code that define printf and other functions) - Compiling: Your C code was translated into a simpler language called assembly
- Assembling: The assembly was translated into the raw numbers (machine code) that your CPU understands
- 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.cThe -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
- Change the message to print your name instead of “Hello, World!”
- Add a second
printfline to print something else - Try removing the
\n- what happens? - What happens if you misspell
printfasprint?
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.