Chapter 1 — Hello, World!
The first C chapter. Through the simplest possible program you experience one full cycle: **C program anatomy → compile → run**.
What you'll learn
- 1Understand the basic structure (`#include`, `main`, `printf`, `return`).
- 2Compile with `gcc` and run from the terminal.
- 3Use escape sequences (`\n`, `\t`) and comments.
Why start with "Hello World"?
Getting one line on screen proves the whole environment (compiler, PATH, encoding) is healthy. That's why almost every intro book starts here — the goal isn't syntax, it's running the dev cycle once end-to-end.
Core Concepts
1) C program skeleton
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}| Line | Meaning |
|---|---|
| `#include <stdio.h>` | **Preprocessor directive** — pull in stdio (for `printf`) |
| `int main(void)` | **Entry point** the OS calls first. Returns `int` |
| `printf("...\n")` | Write to stdout |
| `return 0;` | Tell the OS "exited normally" (exit code 0) |
2) The 4 hidden steps in one `gcc` invocation
hello.c hello (executable)
│ ▲
│ ① preprocess ② compile ③ assemble ④ link │
└─► cpp ─► cc1 ─► as ─► ld ─────┘
(#include) (asm) (object) (libs)`gcc` runs all four for you.
3) Escape sequences
| Notation | Meaning |
|---|---|
| `\n` | Newline |
| `\t` | Tab |
| `\\` | Literal backslash |
| `\"` | Literal double quote |
| `\0` | Null terminator (end of string) |
4) Comments
// single-line comment (C99+)
/* multi-
line comment */The compiler ignores them — they don't affect runtime.
Examples
Example 1 — `ex01_hello.c`: simplest possible output
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}**Output**
Hello, World!Key: drop the `\n` and your next prompt sticks to the same line. Try removing it.
Example 2 — `ex02_multiline.c`: multiple lines and tabs
printf("Line one\n");
printf("Line two\n");
printf("Tab\there\n");
printf("All\nat\nonce\n");**Output**
Line one
Line two
Tab here
All
at
onceKey: one `printf` with multiple `\n` produces multiple lines.
Example 3 — `ex03_comments.c`: comments don't execute
// single-line
/* printf("this line is NOT printed"); */
printf("Comments don't affect compilation.\n");**Output**
Comments don't affect compilation.Key: wrap code in a comment to temporarily disable it.
Common mistakes
- **Missing semicolon** — `printf("hi")` without `;` fails to compile.
- **Missing `#include`** — using `printf` without `<stdio.h>` triggers "implicit declaration".
- **Capitalizing `Main`** — C is case-sensitive; entry must be `main`.
- **Single vs double quotes** — `"x"` is a string, `'x'` is one character.
- **Garbled non-ASCII** — terminal must be UTF-8.
Recap
- A C source has four parts: `#include`, `main`, body, `return`.
- Build: `gcc -o name file.c`; run: `./name`.
- `printf` does not auto-newline — add `\n` yourself.
- Comments are free; use them.
Try it
cd src
gcc -std=c11 -Wall -o ex01 ex01_hello.c && ./ex01
gcc -std=c11 -Wall -o ex02 ex02_multiline.c && ./ex02
gcc -std=c11 -Wall -o ex03 ex03_comments.c && ./ex03💻 Examples
Compilable, runnable examples — see the output yourself.
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}
Hello, World!#include <stdio.h>
int main(void) {
printf("Line one\n");
printf("Line two\n");
printf("Tab\tseparated\n");
printf("All at once\nmulti-line\noutput\n");
return 0;
}
Line one
Line two
Tab here
All
at
once#include <stdio.h>
/*
* comment example.
* comment .
*/
int main(void) {
// single-line comment:
printf("Comments don't affect compilation.\n");
/* printf("This line is NOT printed."); */
return 0;
}
Comments don't affect compilation.📝 Exercises
Try them yourself first, then open the solution to compare.
Problem 1 (hw01.c)
Goal: Write a program that prints exactly this self-introduction.
- Filename: hw01.c
Name: John Doe
Age: 20
Hobby: coding▶Toggle solution
#include <stdio.h>
int main(void) {
printf("Name: John Doe\n");
printf("Age: 20\n");
printf("Hobby: coding\n");
return 0;
}
Name: John Doe
Age: 20
Hobby: codingProblem 2 (hw02.c)
Goal: Print this ASCII art. Remember backslashes need `\\` to escape.
- Filename: hw02.c
/\_/\
( o.o )
> ^ <▶Toggle solution
#include <stdio.h>
int main(void) {
printf(" /\\_/\\\n");
printf(" ( o.o )\n");
printf(" > ^ <\n");
return 0;
}
/\_/\
( o.o )
> ^ <All lecture materials and example code are openly available on GitHub.
View on GitHub ↗