How gcc -E shows preprocessor output
What you'll learn: How to use the -E flag to peek behind the curtain and see exactly how the preprocessor modifies your source code before the compiler ever sees it.
The Secret "First Pass"
Before your C code is converted into a program your computer can run, it undergoes a "find and replace" phase called preprocessing. The preprocessor handles lines starting with #, like #include and #define. Usually, this happens invisibly. You run gcc, and a second later, you have an executable file.
However, if your code isn't behaving as expected—perhaps a macro is expanding weirdly or a header file isn't loading what you thought it would—you can tell the compiler to stop immediately after the preprocessing step. We do this using the -E flag.
Using the -E Flag
Think of gcc -E like an X-ray machine. It ignores the actual "building" of the program and simply spits out the expanded text version of your file.
Imagine you have a file named hello.c:
#define GREETING "Hello, world!"
int main() {
printf(GREETING);
return 0;
}
If you run the command gcc -E hello.c, your terminal will fill with text. You'll see hundreds of lines from stdio.h (the code the preprocessor "copied and pasted" for you), and at the very bottom, you'll see your main function transformed:
int main() {
printf("Hello, world!");
return 0;
}
Notice that #define GREETING is gone, and the word GREETING has been physically replaced by its value. This is exactly what the compiler sees.
Why this matters
The preprocessor is essentially a sophisticated "copy-paster." It doesn't understand C logic; it only understands text manipulation. By using -E, you can debug complex macros or verify which files are being included. If you find the output too long to read in the terminal, you can save it to a file to inspect it closely:
gcc -E hello.c -o preprocessed_output.txt
Opening that text file reveals the "truth" of your code, stripped of all preprocessor shortcuts and directives.
Key takeaway
Use gcc -E to stop the build process early and view the final, expanded text that the preprocessor hands off to the compiler.