The C Path — learn C, visually

🌱 C Basics

Hello, World: your first C program

⏱ 12 min · free interactive lesson · quizzes, visualizations & a real compiler

▶ Open the interactive lesson — free, no signup
Why you're learning this

The operating system on your laptop, the games you play, and the language Python itself were all written in C — and every one of their authors once sat exactly where you are, typing a five-line first program. In the next ten minutes you'll write it, turn it into real machine code, and run it. Part 0 gave you the map; this is where you start the engine.

This is it — the moment every programmer remembers. You're about to write a real program in C, the language that built Unix, Linux, Windows, Python, Git, and probably the firmware in your toaster. And like millions before you, you'll start by making the computer say hello:

hello.c
#include <stdio.h>

int main(void) {
    printf("Hello, World!\n");
    return 0;
}

Five lines. Every single one earns its place. Let's dissect them like the tiny masterpiece they are.

Line 1: #include <stdio.h>

C itself is famously minimal — it doesn't even know how to print! Printing lives in the standard library, and stdio.h (standard input/output header) is the file that declares functions like printf. The #include directive tells the preprocessor: "paste that entire file right here before compiling." Remember the compiler pipeline from Part 0? This is stage 1 doing its find-and-replace magic.

Line 3: int main(void)

Every C program needs exactly one main — it's the agreed-upon entry point. When you run your program, the operating system (well, some startup code) calls main for you. The pieces:

🧠 Checkpoint: Why does every C program need a function called main?

  • It runs faster than other names
  • The compiler deletes all other functions
  • It is the agreed-upon entry point the OS startup code calls
  • It is only needed on Linux
Show answer

It is the agreed-upon entry point the OS startup code calls — Execution always begins at main — that name is baked into the C standard and the startup code linked into your program. No main, no program.

Line 4: printf("Hello, World!\n");

A function call: run the code named printf (print formatted), handing it the text between quotes — a string literal. The \n at the end is an escape sequence meaning "newline": without it, your shell prompt would glue itself to the end of your message. And the semicolon? Every statement in C ends with one. Forgetting it is the classic first-week rite of passage.

💡

Other escape sequences you'll use constantly: \t (tab), \" (a quote inside a string), \\ (a literal backslash). They let you type the untypeable.

Line 5: return 0;

Remember main promised an int? This delivers it. That number is the program's exit status, reported to the operating system: by convention 0 means "all good" and anything nonzero means "something went wrong". Shell scripts, Makefiles, and CI systems all read this value — it's how programs gossip about success and failure.

🧠 Checkpoint: What does return 0; in main actually do?

  • Reports "success" to the operating system as the exit status
  • Prints 0 to the screen
  • Restarts the program
  • Frees all memory
Show answer

Reports "success" to the operating system as the exit status — The return value of main becomes the process exit status. 0 = success, nonzero = failure. Try echo $? in your shell right after running a program to see it!

Making it real: compile and run

C is a compiled language — the source text must be translated into machine code before it can run. That's one command:

terminal
$ gcc hello.c -o hello    # compile: hello.c -> executable named 'hello'
$ ./hello                 # run it (./ means "in this directory")
Hello, World!
$ echo $?                 # ask the shell for the exit status
0

Your development loop, forever after, looks like this:

This spot has an interactive flow widget — open the interactive lesson to play with it.

⚠️

When the compiler yells at you — and it will — read the first error, fix it, recompile. One missing semicolon can trigger an avalanche of bogus follow-up errors. First error first, always.

Your turn — break it, then make it yours

Reading about programs teaches you a little. Changing them teaches you everything. Edit the program below and run it:

This spot has an interactive editor widget — open the interactive lesson to play with it.

🧠 Checkpoint: What does \n inside a string literal mean?

  • A literal backslash and n
  • A newline character
  • The end of the string
  • A space
Show answer

A newline character — It is an escape sequence: the two characters \ and n in your source become ONE character (ASCII 10, newline) in the compiled string. The string terminator is a different character, \0.

You've officially executed your own machine code — welcome to the club. Next, let's give programs a memory: variables and types.

▶ Practice this lesson interactively (with live gcc)