Course contentsShow
← C
Lesson 57 of 1,2191. FoundationsFree lesson

Basic `gcc` command flags

Basic gcc command flags

What you'll learn: How to use specific flags to control the output name and error reporting of the C compiler.

The compiler is your translator

Think of gcc as a professional translator. If you just hand it a document (your .c file) and say "translate this," it will do the job, but it might not follow your specific instructions for how to label the finished book. By using flags—extra bits of text added to the command—you can tell the compiler exactly how you want it to behave.

Naming your output with -o

By default, if you run gcc hello.c, the compiler creates a generic executable file named a.out (or a.exe on Windows). This is messy, especially if you have ten different programs in one folder.

To give your program a specific name, use the -o (output) flag. You provide the flag followed by the name you want.

// Save this as hello.c
#include <stdio.h>

int main() {
    printf("Hello, C world!\n");
    return 0;
}

To compile this into a program specifically called hello, you would type: gcc hello.c -o hello

Now, instead of a.out, you have a clean file named hello ready to run.

Increasing strictness with -Wall

C is a very flexible language, which means it sometimes lets you get away with "sloppy" code that might crash later. The -Wall flag stands for "Warnings: all." It tells the compiler to be extra chatty and warn you about potential mistakes, even if they aren't technical errors that stop the compilation.

For example, if you create a variable but forget to use it, gcc normally stays silent. But if you run gcc -Wall hello.c -o hello, the compiler will give you a friendly heads-up. It’s like having an editor proofread your work for logic errors, not just spelling.

Putting it all together

In the real world, you will rarely run gcc by itself. You will almost always combine these flags to ensure your code is clean and your files are organized:

gcc -Wall main.c -o my_program

This command says: "Compile main.c, show me all possible warnings, and name the final result my_program."

Key takeaway

Use -o to name your executable file and -Wall to let the compiler help you find hidden bugs in your code.