C"Hello, World!" Explained

"Hello, World!" Explained

The previous page got "Hello, World!" running. This page slows down and looks at exactly why each line is written the way it is — the details that seem arbitrary at first but matter as soon as you start writing your own programs.

hello.c

C
#include <stdio.h>

int main(void) {
    printf("Hello, World!\n");
    return 0;
}
What #include actually does

#include <stdio.h> is a preprocessor directive, not a C statement — notice it has no semicolon. Before the compiler ever sees your code, the preprocessor literally copies the entire contents of the stdio.h header file and pastes it in place of that line. That header contains the declaration of printf: its name, the types of arguments it takes, and what it returns. Without it, the compiler would have no idea what printf is, and would refuse to compile the call (or, in older/looser compilers, silently assume a wrong signature).

Why int main(void) and not just main()

int main(void) says two important things explicitly. The int means main returns a whole number back to whatever launched the program (your shell or operating system). The void means main takes no parameters at all. Writing main() with empty parentheses is legal in C but means something subtly different — it declares a function taking an unspecified number of arguments, which is looser and no longer recommended. Being explicit with void communicates intent clearly and lets the compiler catch you if you accidentally pass it arguments somewhere.

What main's return value means to the OS

return 0; doesn't just end the function — that value becomes the program's exit status, which the shell or calling process can inspect. By long-standing convention, 0 means "the program finished successfully," and any non-zero value signals some kind of error. Scripts and other programs routinely check this value (for example, echo $? in a Unix shell right after running your program) to decide whether to continue or abort. It's an easy detail to overlook since you rarely see it directly, but automated tooling depends on it constantly.

Why \n matters

The \n inside the string is an escape sequence representing a newline character. Without it, the next thing your terminal prints — your shell prompt, the next program's output — would appear directly after "Hello, World!" on the same line. Newlines also interact with output buffering: standard output is often line-buffered when connected to a terminal, meaning text isn't guaranteed to actually appear on screen until a newline is written (or the buffer fills, or the program exits). Ending your output with \n is the simplest way to ensure what you print shows up promptly and cleanly formatted.

Don't forget the semicolon
Every C statement — including the call to `printf` — must end with a semicolon. Omitting it produces a compiler error, usually pointing at the line *after* the mistake, since the compiler doesn't realize the statement was supposed to end until it hits something that clearly doesn't belong (like the next line's closing brace). If you get a confusing error on a line that looks perfectly correct, check the line just above it first.
Declarations vs the code that uses them
`stdio.h` only *declares* `printf` — it tells the compiler that the function exists and how to call it. The actual *implementation* of `printf` lives in the C standard library, which gets attached to your program later, during linking. This declaration/implementation split is a recurring theme in C and is covered in depth in the functions section.