Nearby lessons

12 of 124

C - 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.

Do not name it 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:

Example02
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 printf("Hello, World!\n");
6 printf("My first C program.\n");
7 return 0;
8}
Output
Hello, World!
My first C program.

Line by Line

LineMeaning
#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
\nA newline — moves the cursor to the next line
return 0;Tells the operating system the program succeeded
In simple words: #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:

Example04
CCode Cell
1gcc main.c -o main
Output
(no output means success)

Step 4 — Run It

The command differs slightly by shell. All three do the same thing:

Example05
CCode Cell
1# Windows - Command Prompt
2main.exe
3 
4# Windows - PowerShell
5./main.exe
6 
7# Linux / macOS
8./main
Output
Hello, World!
My first C program.

Compile and Run in One Line

Once you are comfortable, chain both steps with && so the program only runs if compiling succeeded:

Example06
CCode Cell
1# Linux / macOS
2gcc main.c -o main && ./main
3 
4# Windows PowerShell
5gcc main.c -o main; if ($?) { ./main.exe }
Output
Hello, World!
My first C program.

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.

MessageReal cause
expected ';' before '}' tokenMissing semicolon on the previous line
'printf' undeclaredForgot #include <stdio.h>
undefined reference to 'main'No main function, or it is misspelled
No such file or directoryWrong filename, or you are in the wrong folder
Permission deniedThe previous .exe is still running — close it
Trainer's Note: A missing semicolon is reported on the line after the mistake, because the compiler only notices something is wrong when it reaches the next token. Always look one line up from where GCC points.
📝 Key Takeaways
  • 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.

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3