C Reference ยท Concepts and Tools
C compiler flags: warnings and optimization
A practical guide to GCC and Clang flags for C: warning sets, optimization levels, debug info, language standard, sanitizers and defining macros from the command line.
A compiler flag is an option you put on the command line to change what the compiler accepts, warns about, and produces. A handful of flags make the difference between a build that silently accepts bugs and one that points at them. The names here are for GCC and Clang, which agree on almost all of them; MSVC uses a different syntax (/W4, /O2, /std:c17).
The right set depends on the goal. While developing you want many warnings, no optimization, debug information and sanitizers. For a release you want optimization and usually no sanitizers. This page lists the flags worth knowing and what each is for.
gcc -std=c23 -Wall -Wextra -Wpedantic -g -O0 main.c -o main # development gcc -std=c23 -Wall -Wextra -O2 -DNDEBUG main.c -o main # release
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Warning flags
| Flag | What it does |
|---|---|
-Wall |
A large set of common-mistake warnings (unused variables, missing return, suspicious format strings, uninitialized use). Despite the name, not "all". |
-Wextra |
More: unused parameters, signed/unsigned comparison, missing field initializers, and others. |
-Wpedantic |
Warns about code that is not standard C (extensions). |
-Wshadow |
A local variable hides another one of the same name. |
-Wconversion |
Implicit conversions that may change a value (noisy, but valuable in numeric code). |
-Wformat=2 |
Stricter checks on printf-style format strings. |
-Wstrict-prototypes |
Functions declared without a prototype (matters before C23). |
-Wvla |
Warns on variable-length arrays. |
-Werror |
Turns every warning into an error. Good in CI; risky for users who build with a newer compiler. |
-Werror=name |
Makes only one warning an error, for example -Werror=implicit-function-declaration. |
-Wno-name |
Disables one warning, for example -Wno-unused-parameter. |
Optimization flags
| Flag | Meaning |
|---|---|
-O0 |
No optimization (default). Fastest build; variables stay where the debugger expects them. |
-O1 |
Basic optimizations. |
-O2 |
The usual release level: most safe optimizations, balanced size and speed. |
-O3 |
More aggressive (more inlining and vectorization); may enlarge code. |
-Os |
Optimize for size. |
-Og |
Optimize while keeping debugging pleasant. |
-flto |
Link-time optimization across files. Needs the flag at compile and link time. |
-march=native |
Use every instruction of the build machine's CPU. The binary may not run on other machines. |
Other common flags
-std=c23(orc17,c11,gnu23): the language standard. Older GCC and Clang call C23-std=c2x. See C standards.-gadds debug information (use-g3to include macros). See gdb.-DNAMEand-DNAME=valuedefine a macro,-UNAMEundefines it.-DNDEBUGdisablesassert.-I diradds a header search directory;-L dirand-lnametell the linker where to look and what library to add. See Compiling and linking.-fsanitize=address,undefinedadds run-time checks. See Sanitizers.-fno-omit-frame-pointerkeeps stack traces readable for profilers and sanitizers.-MMD -MPwrites dependency files that a Makefile can include.-fstack-protector-strongand-D_FORTIFY_SOURCE=2(with-O2) add cheap defenses against buffer overflows; see Secure coding.
Usage notes
- Fix warnings, do not hide them. A clean build under
-Wall -Wextramakes the one new warning obvious. - Optimization changes behavior of broken code. A program with undefined behavior can work at
-O0and fail at-O2. That is the program's fault, not the optimizer's. - Warnings depend on optimization. Some warnings, such as "may be used uninitialized", need the analysis that only runs at
-O1and above. - Order matters for libraries. Put
-lflags after the source and object files that use them. - Different compilers, different names. Clang accepts nearly all of the GCC flags above; MSVC has
/W4,/WX,/O2,/Ziinstead.