Skip to main content

Command Palette

Search for a command to run...

Write a script that runs a C file through the preprocessor and saves the result into another file

Introduction to C programming

Updated
6 min read
Write a script that runs a C file through the preprocessor and saves the result into another file
D

Heya! 👋 I love helping people, and one of the best ways I do this is by sharing my knowledge and experiences. My journey reflects the power of growth and transformation, and I’m here to document and share it with you.

I started as a pharmacist, practicing at a tertiary hospital in the Northern Region of Ghana. There, I saw firsthand the challenges in healthcare delivery and became fascinated by how technology could offer solutions. This sparked my interest in digital health, a field I believe holds the key to revolutionizing healthcare.

Determined to contribute, I taught myself programming, mastering tools like HTML, CSS, JavaScript, React, PHP, and more. But I craved deeper knowledge and practical experience. That’s when I joined the ALX Software Engineering program, which became a turning point. Spending over 70 hours a week learning, coding, and collaborating, I transitioned fully into tech.

Today, I am a Software Engineer and Digital Health Solutions Architect, building and contributing to innovative digital health solutions. I combine my healthcare expertise with technical skills to create impactful tools that solve real-world problems in health delivery.

Imposter syndrome has been part of my journey, but I’ve learned to embrace it as a sign of growth. Livestreaming my learning process, receiving feedback, and building in public have been crucial in overcoming self-doubt. Each experience has strengthened my belief in showing up, staying consistent, and growing through challenges.

Through this platform, I document my lessons, challenges, and successes to inspire and guide others—whether you’re transitioning careers, exploring digital health, or diving into software development.

I believe in accountability and the value of shared growth. Your feedback keeps me grounded and motivated to continue this journey. Let’s connect, learn, and grow together! 🚀

My first day of learning C programming was exciting because I had heard so much about the C programming language and couldn't wait to start.

The very first task that we were given was to write a script that runs a C file through the preprocessor and saves the result into another file.

In this article, I am going to explain how to go about a task like this. To begin with, let's get a practical question to work towards.

Write a script that runs a C file through the preprocessor and saves the result into another file.

Work with the following assumptions:

The C file name will be saved in the variable $MYFILE
The output should be saved in a file called 'interm'

To solve this problem, there are a few things we will need to understand. The most important of these is understanding the question itself. There are three parts to the question that you need to understand to answer correctly.

So, what does the question demand of us?

What does the question actually mean?

If you are a student taking the same course that I am taking or have been following my write-ups, you will know that this is coming right after learning about shell scripting.

This means that the first part of the question that says "write a script that runs a C file" is referring to writing a bash script.

The second part of the question is "runs a C file through the preprocessor". If you know anything about the C language then you will agree with me that it is a compiled language.

Hence, programs written in C need to be compiled before execution. This compilation process is broken down into 4 major steps;

  • Preprocessing
  • Actual Compilation
  • Assembly
  • Linking

Each of the steps above yields a different output, and this question is asking us to write a script that will produce the output from the preprocessing.

Hence, we are going to focus on just the preprocessing in this article. I will address the other steps in subsequent articles where I answer their respective questions.

What is preprocessing in C programming?

In the process of compiling any C program, preprocessing is where all the header files and macros that were used in the program are processed and added to the actual program.

You remember how you always begin your C program with the code below;

#include <stdio.h>

Any part of your C program that begins with # is meant for the preprocessor. The initial code above tells the preprocessor to include the standard input output header (stdio.h) file.

So, the first step that your C program goes through is to go and get that header file and every other header file as well as macros that you introduce in your program.

After this is done, a new file is generated, which will then be passed on for the actual compilation process (yet to be discussed—check next article).

How do I get my C program to be preprocessed?

Knowing the answer to this question, "How do I get my C program to be preprocessed?", will help you solve the practical question that was given at the beginning of this article.

There are a number of compilers that can be used but one common one which is available by default in almost all Linux distributions is the GNU Compiler Collection (GCC).

Hence a simple gcc command can be used to compile (any of the 4 compilation processes mentioned earlier) your C program.

However, by default, the gcc command does all the 4 steps together and gives you the final executable file.

If you are looking for just one or more of the intermediate processes' outcomes, then you would have to introduce some options to the gcc command.

Since we are interested in the preprocessing step in this article, we will focus on the option for preprocessing only and tackle the rest in subsequent write-ups.

What gcc option is used for just preprocessing?

Linux commands can take up several options, but each command has specific mentions and what they mean. You can get details on each command by checking the man pages or even using the help option.

Options for a Linux command are introduced with a single hyphen (-) if it is one character and a double hyphen if it is a complete word (more on this later).

To make sure your gcc command performs just the preprocessing, you will have to give it the -E option. So, a typical command to take a C program through only preprocessing will be like:

gcc -E name_of_file

where name_of_file is the name of the file containing the source code which is expected to be a C program.

Now that you have grasp the understand and how to solve the second part of the question, let's take a look at what the third part means.

The third part of the question "saves the result into another file".

How can I save the preprocessed C file into another file

One of the attributes that the gcc command expects is the name of the output file preceeded by the option (-o).

By default, the gcc command will produce an output with the same name. But if you want it to give the output file a special or different name, then you would have to explicitly tell it to do so.

How you do that is by introducing the option -o with an attribute being the name of the file. In the question that we are solving, we were told that the output file should be called interm. Hence, our command will look like:

gcc -E name_of_file -o interm

The above command simply means, preprocess the C program in the file called name_of_file and give the preprocessed file the name interm.

Now, let's put all this together to solve the problem that we were given.

Solution to the practical question

The question was

Write a script that runs a C file through the preprocessor and saves the result into another file.

Work with the following assumptions:

The C file name will be saved in the variable $MYFILE
The output should be saved in a file called 'interm'

If you don't know how to write a bash script then check out my previous post on how to do that.

It is also worth noting that the C file which is to be our source code has been saved into a variable called $MYFILE. This means that you should replace the name of the source code file in your command with the variable $MYFILE.

The solution thus will be like:

#!/bin/bash
gcc -E $MYFILE -o interm

Conclusion

I hope you enjoyed reading this and made you understand how to solve a problem like this. I would like to hear back from you, is there anything I missed, or explained wrongly or something you want me to elaborate more on?

Share with me what you also think, mind you I am also learning and your feedback will be valuable to me.

Thanks for reading and I will love to connect personally with you. If you are on Twitter then you can send me a DM and let's have a good chat.

Comments (11)

Join the discussion
J

Thanks a lot for this excellent write up.

S

Thanks. It has confirmed the doubts I had. I was very frustrated with this question having deleted repo 3 times if I cannot understand checker. Blessing to you in abundance.

I

I can't say much sir. But honestly this is more than an article to me. I also read your inspiring journey sir, I will trace your steps.

T

I think it a very helpful article and I look forward to more.

G

very helpful

M

Thanks!

Hello thank you for your guidance, i had an idea on how to solve the problem but the most challenging part was using GCC to preprocess and your article helped me get unstuck,

after learning on how gcc can be used in the compilation process am glad that I have a lot to read about gcc and the compilation process thank you.

I
iano3y ago

Reading this has saved me ~2hrs of searching for an answer because I was at "What options should I use with the GCC compiler to get the preprocessor intermediate file?" Then I googled and bumped into this very instrumental article.

1
D

Happy to be of help and I am glad you found it when you needed it.

1
S

Wonderful and educative. Thump-up to the brain behind this explanation.

3
D

Thank you, I am glad that you found it helpful

D

Hello sir, I am a part of ALX Cohort 8 on the brink of dropping out because I am not interested in copy and paste. I want to learn Software Engineering as it is why I am here but it wasn't helping. It all seemed too fast-paced.

But thank God, I found your blog sir. I do not only have an answer but I understand. I know why whatever is what it is and its purpose in the terminal

Thank you, sir.

5
I
iano3y ago

Keep going, Jeremiah.

1
D

Happy to hear that. I believe you can make it once you are determined enough and willing to put in the work. All the best

P

Had the same question and didn't understand till now, you're a life saver. I wont give up.

More from this blog

D

Dr. Ehoneah Obed

75 posts

Software engineer writing about systems: in code, in learning, in life. I reverse-engineer complex problems into frameworks. Pharmacist → SWE → Founder.