← Back to C series
🧱
Basic
C program anatomy · compile · run

Chapter 1 — Hello, World!

The first C chapter. Through the simplest possible program you experience one full cycle: **C program anatomy → compile → run**.

printfmaingccescape
Duration
1-2 hours
Level
📊 Beginner
Prerequisite
🎯 Getting Started
Outcome
Build hello.c with gcc and run ./hello

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

c
#include <stdio.h>

int main(void) {
    printf("Hello, World!\n");
    return 0;
}
LineMeaning
`#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

text
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

NotationMeaning
`\n`Newline
`\t`Tab
`\\`Literal backslash
`\"`Literal double quote
`\0`Null terminator (end of string)

4) Comments

c
// 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

c
#include <stdio.h>
int main(void) {
    printf("Hello, World!\n");
    return 0;
}

**Output**

text
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

c
printf("Line one\n");
printf("Line two\n");
printf("Tab\there\n");
printf("All\nat\nonce\n");

**Output**

text
Line one
Line two
Tab	here
All
at
once

Key: one `printf` with multiple `\n` produces multiple lines.

Example 3 — `ex03_comments.c`: comments don't execute

c
// single-line
/* printf("this line is NOT printed"); */
printf("Comments don't affect compilation.\n");

**Output**

text
Comments don't affect compilation.

Key: wrap code in a comment to temporarily disable it.

Common mistakes

  1. **Missing semicolon** — `printf("hi")` without `;` fails to compile.
  2. **Missing `#include`** — using `printf` without `<stdio.h>` triggers "implicit declaration".
  3. **Capitalizing `Main`** — C is case-sensitive; entry must be `main`.
  4. **Single vs double quotes** — `"x"` is a string, `'x'` is one character.
  5. **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

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

ex01_hello.csimplest possible output
CODE
#include <stdio.h>

int main(void) {
    printf("Hello, World!\n");
    return 0;
}
▶ Output
Hello, World!
ex02_multiline.cmultiple lines and tabs
CODE
#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;
}
▶ Output
Line one
Line two
Tab	here
All
at
once
ex03_comments.ccomments don't execute
CODE
#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;
}
▶ Output
Comments don't affect compilation.

📝 Exercises

Try them yourself first, then open the solution to compare.

Exercise 1

Problem 1 (hw01.c)

Goal: Write a program that prints exactly this self-introduction.

Requirements
  • Filename: hw01.c
Sample I/O
Name: John Doe
Age: 20
Hobby: coding
Toggle solution
SOLUTION
#include <stdio.h>

int main(void) {
    printf("Name: John Doe\n");
    printf("Age: 20\n");
    printf("Hobby: coding\n");
    return 0;
}
▶ Output
Name: John Doe
Age: 20
Hobby: coding
Exercise 2

Problem 2 (hw02.c)

Goal: Print this ASCII art. Remember backslashes need `\\` to escape.

Requirements
  • Filename: hw02.c
Sample I/O
  /\_/\
 ( o.o )
  > ^ <
Toggle solution
SOLUTION
#include <stdio.h>

int main(void) {
    printf("  /\\_/\\\n");
    printf(" ( o.o )\n");
    printf("  > ^ <\n");
    return 0;
}
▶ Output
  /\_/\
 ( o.o )
  > ^ <
Example code / lecture materials

All lecture materials and example code are openly available on GitHub.

View on GitHub ↗