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:
sudo apt update
sudo apt install -y build-essentialThen ask GCC which version you have:
gcc --versionUbuntu 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:
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;
}
CYou 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 declaresprintf. Without it the compiler does not knowprintfexists.int main(void)starts the function every C program begins in.intsays it returns a whole number to the operating system;voidsays it takes no arguments.{and}mark the start and end of the function body.printf("Hello, world!\n");prints the text between the quotes.\nis the newline character. The semicolon ends the statement; C needs one after every statement.return 0;endsmainand 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:
cd ~/hello-c-demo
gcc -Wall -Wextra -o hello hello.c
./hello
echo "exit code: $?"-Wall -Wextraturn on most of GCC's warnings. They cost nothing and catch real mistakes, so use them on every build.-o hellonames the output file. Without it GCC writesa.out../helloruns 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 valuemainreturned.
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):
cd ~/hello-c-demo
gcc -E hello.c -o hello.i
wc -l hello.i
grep -n 'int printf' hello.iYour 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:
cd ~/hello-c-demo
gcc -Wall -Wextra -c hello.c -o hello.o
nm hello.onm 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:
cd ~/hello-c-demo
gcc hello.o -o hello
./hello
ldd helloldd 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:
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.cThe new lines:
#include <string.h>declaresstrcspn.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 returnsNULLif 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 beHello, Adafollowed by a line break and then!.%sinprintfis replaced by the stringname.
Run it, typing your name when asked. Then feed it a name from a pipe, and an empty input:
./greetcd ~/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:
cd ~/hello-c-demo
printf 'greet: greet.c\n\tgcc -Wall -Wextra -o greet greet.c\n' > Makefile
cat MakefileThe file reads:
greet: greet.c
gcc -Wall -Wextra -o greet greet.cThe 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:
cd ~/hello-c-demo
rm -f greet
make
make
echo "Grace" | ./greetFor 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:
cd ~/hello-c-demo
sed 's/\\n");/\\n")/' hello.c > broken.c
gcc -Wall -Wextra -o broken broken.cGCC 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:
cd ~/hello-c-demo
grep -v '#include' hello.c > noinclude.c
gcc -Wall -Wextra -o noinclude noinclude.cYou 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:
cd ~
rm -rf ~/hello-c-demoLeave 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
- See the same build steps with a different standard library and a build system: Hello World in C++.
- Go one level lower and write the program the assembler sees: Hello World in x86-64 assembly.
- Grow the two-line Makefile into a real one: automate tasks with a Makefile.
Related tutorials
- Hello World in Zig: zig run, zig build and zig ccYou will install a signature-checked Zig 0.17.1 on Ubuntu 26.04, write and run Hello World with the current I/O API, build a small project with zig build that handles errors explicitly, and use zig cc to compile a C program for Linux and for Windows.
- Hello World in x86-64 assembly with NASM and Linux system callsYou will write an x86-64 assembly program that prints a line using only Linux system calls, assemble it with NASM, link it with ld, check its exit status, then loop it three times and read its machine code with objdump.
- Hello World in WebAssembly text format with wabt and WasmtimeYou will write a WebAssembly module by hand in the text format, assemble it with wat2wasm, print Hello World through WASI in Wasmtime 50, and call an exported add function from both Wasmtime and Node.js.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials