Practice terminal Ready
main.c

                

Ready

Statements run 0 Lines printed 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

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
Terminal commands you will use (Windows Command Prompt vs Linux / macOS)
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

  1. Easiest: install Code::Blocks — download the setup file whose name contains "mingw"; it includes GCC.
  2. Or install MSYS2 from msys2.org, open "MSYS2 UCRT64" and run: pacman -S mingw-w64-ucrt-x86_64-gcc
  3. Add C:\msys64\ucrt64\bin to the Windows PATH (Settings → "Edit the system environment variables").
  4. Open a NEW Command Prompt and type gcc --version — you should see the version number.
  5. "gcc is not recognized" means the PATH step is missing or the window was opened before it.

Install GCC on macOS or Linux

  1. macOS: open Terminal and run xcode-select --install (installs the compiler; gcc is really Apple clang).
  2. Ubuntu / Debian Linux: sudo apt install build-essential
  3. Fedora: sudo dnf install gcc
  4. Check it: gcc --version
  5. Nothing to install? Use an online compiler such as OnlineGDB or Programiz in the browser.

Choose an editor

  1. Code::Blocks: an all-in-one IDE — write, then press "Build and run" (F9).
  2. VS Code: a modern editor — add the "C/C++" extension and compile in its built-in terminal.
  3. Any plain-text editor works (even Notepad) — never a word processor like Word.
  4. Save files with the .c extension, not .c.txt (turn on "file name extensions" in Windows).
  5. Keep your programs in one folder without spaces in its name, e.g. C:\c
Walkthrough

A compile command, piece by piece

Every line, explained.

1 gcc The compiler program. It runs the preprocessor, compiler, assembler and linker for you.
2 -Wall "Warnings: all" — reports code that is legal but probably wrong. Always use it.
3 -Wextra Even more warnings (e.g. unused parameters).
4 -std=c17 Use the 2017/2018 C standard (C17). Without it gcc picks its own default.
5 hello.c The source file to compile. It must be in the current folder (or give its path).
6 -o hello Name the output program hello (hello.exe on Windows). Without -o: a.exe or a.out.
7
8 hello Windows: run the program in this folder.
9 ./hello Linux / macOS: ./ means "this folder" — the terminal does not look there by itself.
Common mistakes

Spot the bug

Mistakes almost every beginner makes — and how to fix them.

'gcc' is not recognized

✗ Wrong
C:\c> gcc hello.c -o hello
'gcc' is not recognized as an internal
or external command
✓ Right
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

✗ Wrong
(edit hello.c, save)
C:\c> hello
Hello!           <- old message
✓ Right
(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

✗ Wrong
gcc: error: hello.c: No such file or directory
✓ Right
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

✗ Wrong
C:\Users\you> gcc hello.c -o hello
gcc: error: hello.c: No such file or directory
✓ Right
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

✗ Wrong
gcc hello.c -o hello
(no -Wall, warnings never seen)
✓ Right
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.

Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 5 correct
Question 1

Which command compiles hello.c into a program called hello?

Question 2

You compile and gcc prints nothing at all. What does that mean?

Question 3

In hello.c:5:30: error: …, what is 5?

Question 4

You changed hello.c and saved it, then ran the program without compiling. What runs?

Question 5

What does -Wall do?

Assessment

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.