Getting Started
What C is, how a program becomes an executable, and your first build.
C is a small language that sits close to the hardware. It has no garbage collector, no built-in string type, and no safety net for most mistakes — the compiler trusts you to know what you're doing. That sounds intimidating, but it's also why C is worth learning: once you understand it, most other languages start to feel like C with extra decoration.
Every C program starts life as plain text and goes through a pipeline before it can run:
From source code to a running program
- Preprocessing — lines starting with
#are expanded (covered in 0x0E). - Compiling — your C code is translated into assembly, then into machine code for your CPU.
- Linking — your compiled code is joined with library code (like the functions behind
printf) into a single executable file.
A compiler does all three steps for you. The most common ones are gcc and clang. If you don't have one installed, search for "install gcc" plus your operating system.
Your first program
Every C program needs a function called main — it's where execution begins. Save this as hello.c:
1#include <stdio.h>23int main(void) {4printf("Hello, world!\n");5return 0;6}
Then compile and run it from a terminal:
1gcc hello.c -o hello2./hello
Reading the program line by line
#include <stdio.h>pulls in declarations for the standard input/output functions, includingprintf.int main(void)declares a function namedmainthat takes no arguments and returns anint.printf(...)writes text to the terminal.\nis an escape sequence for a newline.return 0;hands control back to the operating system. By convention,0means "the program succeeded."
void or the return type. Modern C requires an explicit return type, and void explicitly says "this function takes no arguments" rather than leaving it unspecified.Statements and semicolons
C doesn't care about line breaks or indentation the way Python does. Every statement ends with a semicolon, and whitespace is only there for humans. This means both programs below behave identically — but only one of them is worth writing:
1int main(void) { printf("hi\n"); return 0; }
Consistent formatting matters purely for readability. Pick a style — this course indents with four spaces — and stick with it.