C Basics · beginner · ~8 min

Compiling with gcc

Compile a single .c file to a runnable binary on Linux/macOS.

Lesson

C source code is text. The gcc (GNU C Compiler) program turns that text into a machine-code executable for your CPU. The typical first command is just gcc hello.c -o hello, which produces a file named hello you can run with ./hello.

Add -Wall -Wextra to enable useful warnings: most beginner bugs (uninitialized variables, comparing the wrong types, missing returns) are caught by these flags. Add -std=c11 to lock yourself to the modern dialect.

Code examples

gcc -O2 -Wall -Wextra -std=c11 hello.c -o hello
./hello

Common mistakes

  • Forgetting -o hello — gcc writes to a.out by default.
  • Ignoring warnings. Treat them as errors with -Werror once you're comfortable.