Part 1 · Lesson 1
Hello, World!
On this page
What you'll learn: how a C program goes from source code to a running program, and what
#include,main,printfandreturndo.Before you start: Nothing. Install a compiler with the setup guide and you are ready.
Welcome to your first lesson in C! The journey to mastering C is challenging but incredibly rewarding. It all begins with this single step.
What Is a Compiler?
A COMPILER is a special program that translates the human-readable C code you
write into the machine code that your computer can execute. The commands in this
course call it cc, the usual name for your system's C compiler: GCC (the GNU
Compiler Collection) on most Linux systems, and Clang on macOS. Both work the
same way for these lessons.
The Program, Line by Line
The whole program is only a few lines long. It starts with this one:
#include <stdio.h>
The #include is a PREPROCESSOR DIRECTIVE. It's a command that runs
before the main compilation process starts. It tells the compiler to find
the file specified in the angle brackets < > and copy its entire content
into the program at that spot.
We are including stdio.h, which stands for "Standard Input/Output Header".
This header file contains declarations for essential functions used for
input and output operations, including the printf function we will use below.
Next comes the MAIN FUNCTION, which holds the rest of the program:
int main(void)
{
printf("Hello, World!\n");
return 0;
}
Every complete C program MUST have exactly one
function named main. The execution of the program officially begins at the
top of the main function. The curly braces { and } mark where the
function's BODY, its block of code, begins and ends.
Let's break down its signature: int main(void)
-
int: This is the RETURN TYPE. It specifies that themainfunction will "return" an integer value to the operating system when it finishes. This integer is an "exit code" that signals whether the program ran successfully. -
main: This is the required NAME for the starting function. -
(void): This specifies the function's PARAMETERS (the inputs it takes).voidis a special keyword that means "nothing". So,(void)tells us that this function takes no arguments or inputs.
printf() is a FUNCTION that "prints formatted" output to your console
(the text window where you'll run this program). This function is made
available to us because we included <stdio.h>.
The text inside the double quotes, "Hello, World!\n", is a STRING LITERAL.
This is the data we are passing as an argument to the printf function.
The \n at the end is a special "escape sequence" that represents a NEWLINE
character. It tells the console to move the cursor to the next line after
printing "Hello, World!".
Finally, the semicolon ; at the end marks the end of a C STATEMENT.
Most lines of executable code in C must end with a semicolon.
The return statement ends the execution of the main function. It sends
a value back to the operating system. By convention, return 0; signals
that the program executed successfully without any errors.
Full Source
/**
* @file 1_hello_world.c
* @brief Part 1, Lesson 1: Hello, World!
* @author dunamismax
* @date 06-15-2025
*
* This file is your very first C program and lesson.
* The lesson is taught through the comments in this file. Read them
* from top to bottom to understand what's happening.
*/
/*
* =====================================================================================
* | - LESSON START - |
* =====================================================================================
*
* Welcome to your first lesson in C! The journey to mastering C is challenging
* but incredibly rewarding. It all begins with this single step.
*
* WHAT IS A COMPILER?
* A COMPILER is a special program that translates the human-readable C code you
* write into the machine code that your computer can execute. For this course,
* we will use GCC (GNU Compiler Collection).
*/
// --- Part 1: Preprocessor Directives ---
/*
* The `#include` is a PREPROCESSOR DIRECTIVE. It's a command that runs
* *before* the main compilation process starts. It tells the compiler to find
* the file specified in the angle brackets `< >` and copy its entire content
* into this file right here.
*
* We are including `stdio.h`, which stands for "Standard Input/Output Header".
* This header file contains declarations for essential functions used for
* input and output operations, including the `printf` function we will use below.
*/
#include <stdio.h>
// --- Part 2: The Main Function ---
/*
* This is the MAIN FUNCTION. Every complete C program MUST have exactly one
* function named `main`. The execution of the program officially begins at the
* top of the `main` function.
*
* Let's break down its signature: `int main(void)`
*
* - `int`: This is the RETURN TYPE. It specifies that the `main` function will
* "return" an integer value to the operating system when it finishes. This
* integer is an "exit code" that signals whether the program ran successfully.
*
* - `main`: This is the required NAME for the starting function.
*
* - `(void)`: This specifies the function's PARAMETERS (the inputs it takes).
* `void` is a special keyword that means "nothing". So, `(void)` tells us
* that this function takes no arguments or inputs.
*/
int main(void)
{ // The opening brace `{` marks the beginning of the function's code block (its body).
// --- Part 3: Statements and Functions ---
/*
* `printf()` is a FUNCTION that "prints formatted" output to your console
* (the text window where you'll run this program). This function is made
* available to us because we included `<stdio.h>`.
*
* The text inside the double quotes, `"Hello, World!\n"`, is a STRING LITERAL.
* This is the data we are passing as an argument to the `printf` function.
*
* The `\n` at the end is a special "escape sequence" that represents a NEWLINE
* character. It tells the console to move the cursor to the next line after
* printing "Hello, World!".
*
* Finally, the semicolon `;` at the end marks the end of a C STATEMENT.
* Most lines of executable code in C must end with a semicolon.
*/
printf("Hello, World!\n");
/*
* The `return` statement ends the execution of the `main` function. It sends
* a value back to the operating system. By convention, `return 0;` signals
* that the program executed successfully without any errors.
*/
return 0;
} // The closing brace `}` marks the end of the function's body.
/*
* =====================================================================================
* | - LESSON END - |
* =====================================================================================
*
* Congratulations on writing your first C program!
*
* HOW TO COMPILE AND RUN THIS CODE:
*
* 1. Open a terminal. On Windows, use the Ubuntu terminal of WSL (the Windows
* Subsystem for Linux), which is how this course runs on Windows.
* 2. Navigate to the directory where you saved this file.
* 3. Use the GCC compiler to translate this source code into an executable program.
* The command below does the following:
* - `gcc`: Invokes the compiler.
* - `-Wall -Wextra`: Enables all and extra warning flags to catch potential errors.
* - `-std=c17`: Asks for the C17 standard of the language. C has been revised
* several times (C89, C99, C11, C17, C23); C17 is supported by every current
* compiler, so it is the version this course teaches.
* - `-o 1_hello_world`: Specifies the output file name should be `1_hello_world`.
* - `1_hello_world.c`: Specifies the input source file to compile.
*
* `gcc -Wall -Wextra -std=c17 -o 1_hello_world 1_hello_world.c`
*
* 4. Run the newly created executable:
* - On Linux/macOS: `./1_hello_world`
* - On Windows (WSL): `./1_hello_world`
*
* You should see "Hello, World!" printed to your console.
*/How to Compile and Run
Runs the lesson in Compiler Explorer (GCC 16.2), nothing to install.
Once, before your first lesson
Install Apple's command line tools. They include the cc compiler: xcode-select --install
New to the terminal? The setup guide walks through it.
Build
$ cc -Wall -Wextra -std=c17 -o 1_hello_world 1_hello_world.c
Run
$ ./1_hello_world
- Run these commands in the folder that holds the lesson's file. Download it as a zip.
Try It Yourself
Practise what this lesson taught. Change a copy of the lesson file, or start a new one.
- Change the message to greet yourself by name, then compile and run it again.
- Print three lines with three
printfcalls, then print the same three lines with oneprintfthat contains three\ns. - Delete the semicolon after the
printfcall and compile. Read the error message: which line does it point at? Put the semicolon back.
Try each one yourself before you look at a worked solution. The solutions,
with the reasoning in their comments, are in
exercises/01-hello-world/
in the repository and
on this lesson's page on the website.
Solutions
One worked solution for each exercise above, with the reasoning in its comments. They stay closed until you open them: have a go first, then compare.
Show the solution to exercise 1 Hide the solution to exercise 1
/**
* @file 1_greet_by_name.c
* @brief Lesson 1, Exercise 1: one possible solution
* @author dunamismax
* @date 11-20-2026
*
* THE EXERCISE:
* Change the message to greet yourself by name, then compile and run it again.
*/
#include <stdio.h>
int main(void)
{
// Only the text between the double quotes changed. Everything else in
// the program (the #include, `main`, the braces, `return 0;`) stays the
// same, because the STRING LITERAL is just data handed to `printf`.
printf("Hello, Ada!\n");
return 0;
}
/*
* The program you run is the EXECUTABLE the compiler made, not the `.c` file.
* If you change the message and run the old executable, you still see the
* old message. Compile again after every change:
*
* gcc -Wall -Wextra -std=c17 -o 1_greet_by_name 1_greet_by_name.c
* ./1_greet_by_name
*/Show the solution to exercise 2 Hide the solution to exercise 2
/**
* @file 2_three_lines.c
* @brief Lesson 1, Exercise 2: one possible solution
* @author dunamismax
* @date 11-20-2026
*
* THE EXERCISE:
* Print three lines with three `printf` calls, then print the same three
* lines with one `printf` that contains three `\n`s.
*/
#include <stdio.h>
int main(void)
{
// First way: one `printf` call per line. Each string ends with `\n`,
// which moves the cursor to the start of the next line.
printf("Roses are red,\n");
printf("Violets are blue,\n");
printf("C is a language.\n");
// Second way: one call. `printf` does not start a new line on its own;
// a new line begins only where the string contains `\n`. So one string
// with three `\n`s prints three lines.
printf("Roses are red,\nViolets are blue,\nC is a language.\n");
return 0;
}
/*
* Both ways print exactly the same characters. Try removing a `\n`: the two
* lines around it run together, because nothing told the console to move
* down.
*
* HOW TO COMPILE AND RUN:
* gcc -Wall -Wextra -std=c17 -o 2_three_lines 2_three_lines.c
* ./2_three_lines
*/Show the solution to exercise 3 Hide the solution to exercise 3
/**
* @file 3_missing_semicolon.c
* @brief Lesson 1, Exercise 3: what you should have seen
* @author dunamismax
* @date 11-20-2026
*
* THE EXERCISE:
* Delete the semicolon after the `printf` call and compile. Read the error
* message: which line does it point at? Put the semicolon back.
*
* This file is the program with the semicolon back in place, so it compiles.
* The comments describe what the compiler says without it.
*/
#include <stdio.h>
int main(void)
{
printf("Hello, World!\n"); // <-- the semicolon the exercise removes
return 0;
}
/*
* WHAT THE COMPILER SAYS
*
* Without the semicolon, GCC stops with an error like this (the first
* number is the line, the second is the column):
*
* hello.c:5:30: error: expected ';' before 'return'
* 5 | printf("Hello, World!\n")
* | ^
* | ;
*
* Clang words it differently but means the same thing:
*
* hello.c:5:30: error: expected ';' after expression
*
* HOW TO READ IT
*
* - It is an ERROR, not a warning: no executable is made. If an old
* executable is still in the directory, running it shows the old program.
* - "expected ';' before 'return'" tells you how the compiler found out. It
* did not notice anything wrong at the end of the `printf` line; it only
* became confused when it reached the next word, `return`. Current
* compilers still point at the `printf` line and even show where the `;`
* belongs. Older ones point at the `return` line instead.
* - That is the habit to take from this exercise: when the line an error
* names looks fine, look at the end of the line BEFORE it.
*
* HOW TO COMPILE AND RUN:
* gcc -Wall -Wextra -std=c17 -o 3_missing_semicolon 3_missing_semicolon.c
* ./3_missing_semicolon
*/Finished this lesson?
Mark it complete to track your progress through the course.