Skip to main content

Hello World (first programs) · Beginner · 15 min

Hello World in C: compile and link your first program with GCC

By Padding · Updated 18 Nov 2026
GCC 16.2 · gcc:16 image (Debian 13) · Ubuntu 26.04 ships GCC 15.2

You will write, compile and run a C program with GCC, split the build into preprocess, compile and link so you can see each stage, then read a name safely from the keyboard and build it with a two-line Makefile.

On this page

C is the language under most of the software you use without noticing: the Linux kernel, the SQLite database inside every phone, the reference Python interpreter (CPython), and the firmware in routers, meters and washing machines. Learning it teaches you what a computer is actually doing when a program runs.

C has no interpreter and no virtual machine. A compiler turns your source file into machine code for your processor, and the result is a file the operating system runs directly. The compiler here is GCC, the GNU Compiler Collection. The gcc command is really a driver: it calls a preprocessor, the compiler proper, an assembler and a linker in turn. In this tutorial you run those stages one at a time, so "compile" stops being a magic word.

You work in a throwaway folder, ~/hello-c-demo, and delete it at the end.

Step 1 · Install GCC

On Ubuntu 26.04, install the build-essential package. It brings GCC, the C standard library headers and make:

Bash
sudo apt update
sudo apt install -y build-essential

Then ask GCC which version you have:

Bash
gcc --version

Ubuntu 26.04 installs GCC 15.2. The newest release is GCC 16.2, from August 2026. Everything in this tutorial works the same on either, because both compile C to the C23 standard by default.

On Windows, the simplest route is WSL: install Ubuntu from the Microsoft Store and follow the Ubuntu commands. If you prefer native Windows programs, MSYS2 (msys2.org) provides GCC; in its UCRT64 terminal run pacman -S mingw-w64-ucrt-x86_64-gcc make. On macOS, xcode-select --install installs Apple's Clang compiler, which also answers to the name gcc; the commands below work, but the error messages read differently.

Check it worked

The first line of the output names the version, for example gcc (Ubuntu 15.2.0-...) 15.2.0 on Ubuntu or gcc (GCC) 16.2.0 with GCC 16. If you see gcc: command not found, the install did not finish; run the apt install line again and read its last lines for an error.

Step 2 · Write hello.c

Make the demo folder and create the source file:

Bash
mkdir -p ~/hello-c-demo
cd ~/hello-c-demo
cat > hello.c <<'C'
#include <stdio.h>

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

You can type the same seven lines into a text editor instead and save them as hello.c. Here is what each line does:

  • #include <stdio.h> is an instruction to the preprocessor, not to the compiler. It pastes in the contents of the standard input/output header, which declares printf. Without it the compiler does not know printf exists.
  • int main(void) starts the function every C program begins in. int says it returns a whole number to the operating system; void says it takes no arguments.
  • { and } mark the start and end of the function body.
  • printf("Hello, world!\n"); prints the text between the quotes. \n is the newline character. The semicolon ends the statement; C needs one after every statement.
  • return 0; ends main and hands 0 back to the operating system. By convention 0 means success and any other number means something went wrong.

Check it worked

cat hello.c prints the seven lines exactly as above, with the #include line first.

Step 3 · Compile, run and read the exit code

Compile the file into a program called hello, then run it:

Bash
cd ~/hello-c-demo
gcc -Wall -Wextra -o hello hello.c
./hello
echo "exit code: $?"
  • -Wall -Wextra turn on most of GCC's warnings. They cost nothing and catch real mistakes, so use them on every build.
  • -o hello names the output file. Without it GCC writes a.out.
  • ./hello runs the program from the current folder. The ./ is needed because the shell does not look in the current folder for commands.
  • $? holds the exit code of the last command, which is the value main returned.

Check it worked

GCC prints nothing when there are no warnings or errors. The program prints Hello, world! and the next line reads exit code: 0. Change return 0; to return 3;, compile and run again, and you see exit code: 3. Put it back to 0 before you continue.

Step 4 · Run the build one stage at a time

The single gcc command ran four programs in turn: the preprocessor, the compiler proper, the assembler and the linker. Run them in three steps. First, the preprocessor alone (-E):

Bash
cd ~/hello-c-demo
gcc -E hello.c -o hello.i
wc -l hello.i
grep -n 'int printf' hello.i

Your seven-line file has become several hundred lines: #include pasted the whole of stdio.h (and the headers it includes) into it. The grep finds the declaration of printf that came with it.

Next, compile and assemble to an object file (-c). This turns C into machine code but does not make a runnable program yet:

Bash
cd ~/hello-c-demo
gcc -Wall -Wextra -c hello.c -o hello.o
nm hello.o

nm lists the names in the object file. T main means main is defined here. A U line means "used here but defined elsewhere": you will see U puts or U printf. GCC often swaps a printf of a fixed string ending in \n for the simpler puts, which does the same job. Either way, the code for it is not in your file.

Finally, link. The linker joins your object file with the C library, which holds the real printf or puts:

Bash
cd ~/hello-c-demo
gcc hello.o -o hello
./hello
ldd hello

ldd shows the shared libraries the program loads when it starts. One of them is libc.so.6, the C library on Linux systems that use glibc.

Add -v to any gcc command if you want to see every program the driver runs, including cc1 (the compiler proper), as (the assembler) and collect2/ld (the linker).

Check it worked

wc -l reports hundreds of lines for hello.i. nm hello.o shows T main and a U line for puts or printf. After linking, ./hello prints Hello, world! again and ldd lists libc.so.6.

Step 5 · Read a name safely with fgets

Now a program that asks for your name. C strings live in fixed-size arrays, so reading input means making sure it cannot overflow the array. fgets takes the array size and never writes past it:

Bash
cd ~/hello-c-demo
cat > greet.c <<'C'
#include <stdio.h>
#include <string.h>

int main(void)
{
    char name[64];

    printf("What is your name? ");
    fflush(stdout);

    if (fgets(name, sizeof name, stdin) == NULL) {
        fprintf(stderr, "No name given.\n");
        return 1;
    }

    name[strcspn(name, "\n")] = '\0';
    printf("Hello, %s!\n", name);
    return 0;
}
C
gcc -Wall -Wextra -o greet greet.c

The new lines:

  • #include <string.h> declares strcspn.
  • char name[64]; reserves room for 64 bytes. A C string ends with a zero byte, so it holds at most 63 bytes of text.
  • fflush(stdout); makes sure the question appears before the program waits. Output without a newline can sit in a buffer otherwise.
  • fgets(name, sizeof name, stdin) reads one line from the keyboard (standard input), up to 63 bytes, and keeps the newline you typed. It returns NULL if there is nothing to read, so the program reports it on standard error and exits with 1.
  • name[strcspn(name, "\n")] = '\0'; finds the position of the first newline (or the end of the string if there is none) and replaces it with the end-of-string byte. Without this line the output would be Hello, Ada followed by a line break and then !.
  • %s in printf is replaced by the string name.

Run it, typing your name when asked. Then feed it a name from a pipe, and an empty input:

Bash
./greet
Bash
cd ~/hello-c-demo
echo "Ada" | ./greet
printf 'Adébáyọ̀\n' | ./greet
./greet < /dev/null || echo "exit code: $?"

The 64 is bytes, not letters. In UTF-8, the text encoding Linux terminals use, a Yoruba name such as Adébáyọ̀ takes 13 bytes for its 7 letters: the accented letters need 2 or 3 bytes each, and the low-tone mark on ọ is stored as a separate character. The same applies to Amharic, Arabic or Chinese text. Size buffers for bytes, and remember that strlen counts bytes too.

Check it worked

The piped runs print What is your name? Hello, Ada! and What is your name? Hello, Adébáyọ̀! with the ! on the same line as the name. The empty input prints No name given. and exit code: 1. Typing a name longer than 63 characters does not crash the program; it greets you with the first 63 bytes.

Step 6 · Build with a two-line Makefile

Typing the full gcc command each time gets old. make reads a file called Makefile that says how to build each file and rebuilds only when the source has changed. Create one with two lines:

Bash
cd ~/hello-c-demo
printf 'greet: greet.c\n\tgcc -Wall -Wextra -o greet greet.c\n' > Makefile
cat Makefile

The file reads:

Text
greet: greet.c
	gcc -Wall -Wextra -o greet greet.c

The first line says greet is built from greet.c. The second line is the command, and it must start with a tab character, not spaces. The printf above writes the tab as \t so a copy and paste from this page cannot turn it into spaces.

Delete the program, build it with make, then run make again:

Bash
cd ~/hello-c-demo
rm -f greet
make
make
echo "Grace" | ./greet

For targets, variables and more rules, see automate tasks with a Makefile.

Check it worked

The first make prints the gcc command as it runs it. The second prints make: 'greet' is up to date. and does nothing, because greet.c has not changed since. The last line prints What is your name? Hello, Grace!. If make says missing separator, the second line of Makefile starts with spaces instead of a tab.

Step 7 · Recognise the errors beginners hit

Make each mistake on purpose so you know the message when it happens by accident.

A missing semicolon. Copy hello.c, delete the semicolon after the printf line, and compile:

Bash
cd ~/hello-c-demo
sed 's/\\n");/\\n")/' hello.c > broken.c
gcc -Wall -Wextra -o broken broken.c

GCC reports something like error: expected ';' before 'return' (in a UTF-8 terminal the quotes may be curly). It only notices the problem when it reaches the next token, return on the following line, so the message talks about that line. When an error makes no sense, look at the end of the line above it.

A missing include. Remove the #include <stdio.h> line and compile:

Bash
cd ~/hello-c-demo
grep -v '#include' hello.c > noinclude.c
gcc -Wall -Wextra -o noinclude noinclude.c

You get something like error: implicit declaration of function 'printf', with a note suggesting #include <stdio.h>. Older guides call this a warning; since GCC 14 it is an error and no program is produced, because calling a function the compiler has never seen declared leads to wrong code.

A forgotten \n. Remove \n from the printf text and run the program. It still works, but your shell prompt appears straight after Hello, world! on the same line. Terminal output is line-based, so end what you print with a newline.

Check it worked

You have seen something like expected ';' before 'return' and implicit declaration of function 'printf' in your own terminal, and neither command produced a program: ls broken noinclude reports that both files do not exist.

Step 8 · Clean up

Delete the demo folder and everything in it:

Bash
cd ~
rm -rf ~/hello-c-demo

Leave build-essential installed if you plan to carry on with C, C++ or anything that compiles native code. To remove it, run sudo apt remove build-essential.

Check it worked

ls ~/hello-c-demo reports No such file or directory.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.