How does preprocessed code look?
You can use the -E
option with the GCC compiler to stop after preprocessing and display the preprocessed code. Here's a simple example:
Example: Preprocessor Directives with GCC
// main.c
#include <stdio.h>
// Define a constant
#define PI 3.14159
// Define a macro function
#define SQUARE(x) ((x) * (x))
int main() {
int radius = 5;
float area = PI * SQUARE(radius);
printf("Area of the circle: %f\n", area);
return 0;
}
To view the preprocessed code, you can use the following command in your terminal:
gcc -E main.c -o main_preprocessed.c
This will preprocess the code and save the result in a file named main_preprocessed.c
. You can then open this file to see the preprocessed code:
cat main_preprocessed.c
The preprocessed code will look something like this:
// contents of stdio.h will be pasted here
// I'm just showing the line that declares the printf function
// in the stdio.h header file
extern int printf (const char *__restrict __format, ...);
// rest of stdio.h
int main() {
int radius = 5;
float area = 3.14159 * ((radius) * (radius));
printf("Area of the circle: %f\n", area);
return 0;
}
This is just a snippet of the preprocessed code. You can see how the preprocessor has replaced the macro PI
with its value and expanded the SQUARE
macro in the code. The preprocessed code includes all the header files and macro substitutions, giving you a clearer view of what the compiler sees before actual compilation.
Subscribe to my newsletter
Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Jyotiprakash Mishra
Jyotiprakash Mishra
I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.