Convert C/C++ program to Preprocessor code

Here we will see how to generate the preprocessed or preprocessor code from the source code of a C or C++ program.

To see the preprocessed code using gcc compiler, we have to use the '-E' option with the gcc. The preprocessor includes all of the # directives in the code, and also expands the MACRO function.

Syntax

gcc -E program.c

Example

Let's create a simple C program with macro definitions and see how the preprocessor expands them −

#include <stdio.h>
#define PI 3.1415
#define SQUARE(x) ((x) * (x))

int main() {
    float radius = 5.0;
    float area = PI * SQUARE(radius);
    printf("Area of circle: %.2f\n", area);
    return 0;
}
Area of circle: 78.54

Preprocessed Output

When we run gcc -E program.c, the preprocessor expands all macros and includes header files. The relevant part of the output shows −

int main() {
    float radius = 5.0;
    float area = 3.1415 * ((radius) * (radius));
    printf("Area of circle: %.2f\n", area);
    return 0;
}

Key Points

  • The -E flag stops compilation after preprocessing
  • All #define macros are replaced with their actual values
  • Header files are included and expanded inline
  • Comments are removed from the preprocessed code

Conclusion

Using gcc -E is helpful for debugging macro expansions and understanding what the preprocessor does before actual compilation begins.

Updated on: 2026-03-15T10:30:47+05:30

543 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements