Learning C
- Write a script that runs a C file through the preprocessor and save the result into another file. The C file name will be saved in the variable $CFILE The output should be saved in the file c
The start of my journey into C programming was filled with anticipation due to the buzz surrounding the language.
The initial assignment was intriguing: crafting a script to pass a C file through the preprocessor and store the output in a separate file.
In this article, I'll detail the process of tackling such a task.
To address this challenge, several concepts necessitate comprehension.
Foremost among them is grasping the essence of the inquiry. The question comprises three components crucial for accurate response.
Hence, what exactly does the question require from us?
What does the question actually mean?
For those enrolled in the same course or who have been following my content, it's important to recognize that this question arises after the exploration of shell scripting.
Consequently, when the question prompts us to "create a script that executes a C file," it specifically involves devising a bash script.
The subsequent query involves "processing a C file through the preprocessor." Anyone familiar with the C language will concur that it's a compiled language.
As a result, C programs necessitate compilation before they can be executed. This compilation procedure unfolds in four distinct phases:
- Preprocessing
- Actual Compilation
- Assembly
- Linking Each phase generates a distinct output, and the question at hand requests the creation of a script to produce the output from the preprocessing stage.
Thus, our present focus centers solely on preprocessing within this article. The other phases will be addressed in forthcoming articles, each dedicated to responding to their respective inquiries.
What is preprocessing in C programming?
During the compilation process of a C program, preprocessing is the stage where all the header files and macros utilized in the program are processed and incorporated into the main program.
Recall the customary initiation of a C program with the following lines:
Any section of your C program initiated with the # symbol is targeted for the preprocessor. In the initial code provided above, the preprocessor is instructed to include the standard input-output header file, stdio.h.
Consequently, the first phase that your C program undergoes involves acquiring the specified header file along with any other headers and macros introduced within the program.
Subsequent to this process, a new file is generated. This newly formed file then proceeds to the subsequent stage of the compilation process, which will be elaborated on in the 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 come with various settings, but each command has particular notes on what these settings do. You can learn about each command by looking at the manual pages or using the help option.
The settings for a Linux command are indicated with a single hyphen (-) if it's a single character, and a double hyphen if it's a complete word (we'll delve into this further later on).
If you want to ensure that your "gcc" command only does preprocessing, you need to use the -E setting. Therefore, a typical command to run just the preprocessing step for a C program will look something like this:
gcc -E name_of_file
In the "name_of_file" part, you should put the name of the file that holds your source code, and this file should contain a C program.
Now that you've gotten a handle on how to comprehend and tackle the second part of the question, let's explore what the third part signifies.
The third aspect of the question, "saves the result into another file," means that after you've done the necessary work on your C program, you need to store the outcome in a different file.
How can I save the preprocessed C file into another file?
One of the things that the gcc command needs is the name of the file where it should put the result. You give this name by using the (-o) option.
Normally, gcc makes the result with the same name as your source file. But if you want to name the result file something special or different, you need to tell gcc clearly.
You do this by adding (-o) and then the name you want for the file. In the question we're working on, they said the result file should be named 'c'. So, our command will be like this:
gcc -E name_of_file -o c
The above command simply means, preprocess the C program in the file called name_of_file and give the preprocessed file the name c.
Now, let's put all this together to solve the problem that we were given.
Solution to the practical question.
!/bin/bash
gcc -E $CFILE -o c
Conclusion
I hope you enjoyed reading this and that it helped you understand how to solve a problem like this. I'm curious to hear from you if I missed anything, explained something incorrectly, or if there's something you'd like me to explain more about.
Remember, I'm also learning, and your thoughts would be really helpful to me. Thank you for sharing your feedback!
Top comments (0)