Home

GCC Compiler Flags Explained: From Warnings to Sanitizers

The GCC compiler accepts dozens of command-line flags that change how your C code is built, checked, and optimized. Knowing the most useful ones turns the compiler from a silent translator into an active safety net. Below is a practical reference of the flags that matter most, followed by a worked example showing how they catch a real bug.

1. Basic Compilation & Output Control

2. Warning Flags (Your Safety Net)

3. Debugging & Code Analysis

4. Optimization Levels (For Speed or Size)

5. Language Standards

6. Runtime Sanitizers


Here is a simple C snippet to illustrate how a common bug can be caught using these safety options:

#include <stdio.h>

int main(void) {
    int secret_number; // Uninitialized variable!

    // Without flags, this might silently print garbage memory
    // Compiling with 'gcc -Wall -Wextra -Werror' turns this into a build-stopping error
    printf("The secret number is: %d\n", secret_number);

    return 0;
}

Let’s look at exactly what happens when you compile that code with different flags.

1. Default Compilation (No Flags)

If you run:

gcc main.c -o program

The compiler will likely remain silent and build the executable successfully.

When you run ./program, the output is unpredictable. Because secret_number was never assigned a value, it contains whatever “garbage” data happened to be left over in that memory location 🗑️.

The secret number is: 32764

(Note: The actual number will vary every time you run it or change machines).

2. Adding Warning Flags (-Wall -Wextra)

If you run:

gcc -Wall -Wextra main.c -o program

The compiler detects the issue and prints a warning to your terminal, but it still completes the build and creates the executable.

main.c: In function 'main':
main.c:8:5: warning: 'secret_number' is used uninitialized [-Wuninitialized]
    8 |     printf("The secret number is: %d\n", secret_number);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.c:4:9: note: 'secret_number' was declared here
    4 |     int secret_number; // Uninitialized variable!
      |         ^~~~~~~~~~~~~

3. Strict Quality Control (-Wall -Wextra -Werror)

If you add the error flag:

gcc -Wall -Wextra -Werror main.c -o program

The warning matches the rule, and -Werror instantly upgrades it. The compiler treats it as a critical failure and refuses to generate the executable file.

main.c: In function 'main':
main.c:8:5: error: 'secret_number' is used uninitialized [-Werror=uninitialized]
    8 |     printf("The secret number is: %d\n", secret_number);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.c:4:9: note: 'secret_number' was declared here
    4 |     int secret_number; // Uninitialized variable!
      |         ^~~~~~~~~~~~~
cc1: all warnings being treated as errors
Tags: CGccCompilation