Setting Up: Write, Compile & Run
Your C workshopTo program in C you need two things: an editor to write the code and a compiler to turn it into a program. In the practice terminal below you compile and run programs, read error messages, switch on warnings — and find out why you must compile again after every change.
- CompilerGCC — free, on every system
- Compilegcc hello.c -o hello
- Runhello (Windows) · ./hello
Ready
Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode
The ideas behind the lab
Exactly what you watched happen, in plain steps.
The edit → compile → run cycle
Every C program is made the same way. You write the code in an editor and save it as a .c file. The compiler (gcc) checks it and turns it into a program file. Then you run that program. Each time you change the code you must save, compile and run again — the program file does not update itself.
1. Edit write hello.c in an editor, save (Ctrl+S)
2. Compile gcc -Wall hello.c -o hello
3. Run hello (Windows)
./hello (Linux / macOS)
Error or warning? fix the code → save → compile again
| To do this | Windows | Linux / macOS |
|---|---|---|
| Check that gcc is installed | gcc --version | gcc --version |
| Go to your folder | cd C:\c | cd ~/c |
| Make a new folder | mkdir c | mkdir c |
| List the files | dir | ls |
| Compile (with warnings) | gcc -Wall hello.c -o hello | gcc -Wall hello.c -o hello |
| Run the program | hello (PowerShell: .\hello) | ./hello |
| Show the exit code | echo %errorlevel% | echo $? |
| Clear the screen | cls | clear |
| Stop a program that hangs | Ctrl+C | Ctrl+C |
Install GCC on Windows
- Easiest: install Code::Blocks — download the setup file whose name contains "mingw"; it includes GCC.
- Or install MSYS2 from msys2.org, open "MSYS2 UCRT64" and run: pacman -S mingw-w64-ucrt-x86_64-gcc
- Add C:\msys64\ucrt64\bin to the Windows PATH (Settings → "Edit the system environment variables").
- Open a NEW Command Prompt and type gcc --version — you should see the version number.
- "gcc is not recognized" means the PATH step is missing or the window was opened before it.
Install GCC on macOS or Linux
- macOS: open Terminal and run xcode-select --install (installs the compiler; gcc is really Apple clang).
- Ubuntu / Debian Linux: sudo apt install build-essential
- Fedora: sudo dnf install gcc
- Check it: gcc --version
- Nothing to install? Use an online compiler such as OnlineGDB or Programiz in the browser.
Choose an editor
- Code::Blocks: an all-in-one IDE — write, then press "Build and run" (F9).
- VS Code: a modern editor — add the "C/C++" extension and compile in its built-in terminal.
- Any plain-text editor works (even Notepad) — never a word processor like Word.
- Save files with the .c extension, not .c.txt (turn on "file name extensions" in Windows).
- Keep your programs in one folder without spaces in its name, e.g. C:\c
A compile command, piece by piece
Every line, explained.
gcc
The compiler program. It runs the preprocessor, compiler, assembler and linker for you.
-Wall
"Warnings: all" — reports code that is legal but probably wrong. Always use it.
-Wextra
Even more warnings (e.g. unused parameters).
-std=c17
Use the 2017/2018 C standard (C17). Without it gcc picks its own default.
hello.c
The source file to compile. It must be in the current folder (or give its path).
-o hello
Name the output program hello (hello.exe on Windows). Without -o: a.exe or a.out.
hello
Windows: run the program in this folder.
./hello
Linux / macOS: ./ means "this folder" — the terminal does not look there by itself.
Spot the bug
Mistakes almost every beginner makes — and how to fix them.
'gcc' is not recognized
C:\c> gcc hello.c -o hello 'gcc' is not recognized as an internal or external command
Add the compiler's bin folder to PATH, open a NEW terminal, then: C:\c> gcc --version
The terminal cannot find gcc: it is not installed, or its folder is not on the PATH. Terminals opened before you changed PATH do not see the change.
Running the old program
(edit hello.c, save) C:\c> hello Hello! <- old message
(edit hello.c, save) C:\c> gcc -Wall hello.c -o hello C:\c> hello
The program is a separate file built from the code as it was when you compiled. After every change: save, compile, run.
The file is really hello.c.txt
gcc: error: hello.c: No such file or directory
Rename hello.c.txt to hello.c (show file name extensions in Explorer)
Notepad adds .txt unless you choose "All files". Windows hides extensions, so the file looks like hello.c but is not.
Wrong folder
C:\Users\you> gcc hello.c -o hello gcc: error: hello.c: No such file or directory
C:\Users\you> cd C:\c C:\c> gcc hello.c -o hello
gcc looks for hello.c in the folder the terminal is in. Use cd to go to the folder where you saved it.
Ignoring warnings
gcc hello.c -o hello (no -Wall, warnings never seen)
gcc -Wall hello.c -o hello (read and fix every warning)
A warning does not stop the build, but it usually points at a real bug such as a wrong format specifier.
Check your understanding
Answer each question — you will see the explanation straight away.
Which command compiles hello.c into a program called hello?
gcc is the compiler, hello.c is the source file and -o hello gives the output program its name.
You compile and gcc prints nothing at all. What does that mean?
Compilers are quiet when everything is fine. Errors and warnings are the only messages.
In hello.c:5:30: error: …, what is 5?
The format is file:line:column — line 5, column 30.
You changed hello.c and saved it, then ran the program without compiling. What runs?
The program file only changes when you compile again.
What does -Wall do?
-Wall enables most warnings. They do not stop the build, but they point at likely bugs.
Setting Up: Write, Compile & Run quiz
Test yourself: 10 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.