Skip to content
Tutorials Center
My learning

Topics

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.

TEXT
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

C Macros that flags control

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.

C Writing code that passes -Wall -Wextra

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 (or c17, c11, gnu23): the language standard. Older GCC and Clang call C23 -std=c2x. See C standards.
  • -g adds debug information (use -g3 to include macros). See gdb.
  • -DNAME and -DNAME=value define a macro, -UNAME undefines it. -DNDEBUG disables assert.
  • -I dir adds a header search directory; -L dir and -lname tell the linker where to look and what library to add. See Compiling and linking.
  • -fsanitize=address,undefined adds run-time checks. See Sanitizers.
  • -fno-omit-frame-pointer keeps stack traces readable for profilers and sanitizers.
  • -MMD -MP writes dependency files that a Makefile can include.
  • -fstack-protector-strong and -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 -Wextra makes the one new warning obvious.
  • Optimization changes behavior of broken code. A program with undefined behavior can work at -O0 and 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 -O1 and above.
  • Order matters for libraries. Put -l flags 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, /Zi instead.