Nearby lessons
12 of 124C - Create & Run C Program
Write, compile and run your first C program. Learn the gcc command line, how to execute the result on Windows and Linux, and how to read the compiler errors you will inevitably hit.
Step 1 — Create the File
In your project folder, create a new file named main.c. The .c extension is what tells GCC to treat it as C source code.
main.c.txt. Windows hides known extensions by default, so a file that looks like main.c may really be main.c.txt. Turn on File name extensions in Explorer's View tab if compiling says "no such file".Step 2 — Write the Program
Type this in, then save with Ctrl + S. Each line is explained below:
Line by Line
| Line | Meaning |
|---|---|
#include <stdio.h> | Brings in the standard input/output header so printf is available |
int main() | The starting point of every C program; returns an int to the OS |
{ } | Mark the start and end of the function body |
printf(...) | Prints text to the screen |
\n | A newline — moves the cursor to the next line |
return 0; | Tells the operating system the program succeeded |
#include borrows tools, main is where the work happens, and return 0 reports success.Step 3 — Compile
Open the integrated terminal with Ctrl + ` and run:
Step 4 — Run It
The command differs slightly by shell. All three do the same thing:
Compile and Run in One Line
Once you are comfortable, chain both steps with && so the program only runs if compiling succeeded:
The One-Keypress Route
If you created tasks.json in the previous lesson, you can skip the terminal entirely:
- Ctrl + Shift + B — build the file that is currently open.
- F5 — build, then run under the debugger.
- Ctrl + Alt + N — build and run, if you installed Code Runner.
Reading Compiler Errors
Errors are normal. GCC always tells you the file, the line, and the column. Fix the first error and recompile — later errors are frequently caused by the first one.
| Message | Real cause |
|---|---|
expected ';' before '}' token | Missing semicolon on the previous line |
'printf' undeclared | Forgot #include <stdio.h> |
undefined reference to 'main' | No main function, or it is misspelled |
No such file or directory | Wrong filename, or you are in the wrong folder |
Permission denied | The previous .exe is still running — close it |
- Source files must end in .c
- gcc main.c -o main compiles; ./main runs it.
- On Windows run it as main.exe or ./main.exe
- Save the file before compiling — gcc reads the disk, not the editor.
- The first error message is the one to fix; later ones are often side effects.