Preprocessor in C is not a part of the compilation process but done just before compilation. Let’s take an example. Suppose you want to go somewhere on your bike. So, going to a random place is part of your processing, whereas picking up the keys of the bike, turning on the ignition, and sitting on the bike is a part of preprocessing. So, this is just an example of preprocessing. Here, we are going to discuss many more interesting concepts of the preprocessors in C:
- What are preprocessors in C?
- What are the types of preprocessors?
- How do preprocessors work in C?
- List of preprocessors
Apart from these topics, we have included an interactive quiz on C preprocessors.
1. What are Preprocessors in C?
The term “preprocessor” is self-explanatory. The word “pre” means “before” and the word “processor” refers to “making something”. Before the source code is compiled, it gets automatically processed due to the presence of preprocessor directives.
In programming terminology, a preprocessor is nothing but a System Software that performs the processing of a high-level language before compilation by translating the source code written in a high-level language to object code written in the machine-level language, that is easily understood by the compiler.
The source code is written in the form of test.c with .c extension where “test” is the name of the file. This file is then processed with the help of preprocessors in C expanding the source code file. After the source code file has been expanded, the next step would be to compile the code that would produce an object code file with the extension .obj and therefore would be named as test.obj. This object code file is then linked to the Standard Library Functions to finally generate the file with extension .exe that would be named as test.exe that can be executed.
Example of Preprocessors in C
Let us acknowledge the existence of preprocessors by considering a simple example. Suppose you forgot to use the hash/pound (#) symbol while working with the header file #include<stdio.h>, will your program work?
In the C programming language, you would find an error if you skip the # sign while defining a header file.
include<stdio.h> // Header file without the hash/pound (#) symbol int main() { printf("Welcome to DataFlair tutorials!\n\n"); return 0; }
Code on Screen
Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!
But, what is the reason behind this error? Why is the hash/pound (#) symbol so important? In order to answer these questions, take a quick tour of this tutorial made super easy for beginners!
2. What are Preprocessor Directives in C?
Preprocessor Directives are nothing but the commands we use in the preprocessor.
Key takeaway: In the C language, all of the preprocessor directives begin with a hash/pound (#) symbol.
We know that in C we can use preprocessor directives anywhere in our program. But, it is preferable to use them at the beginning of the program. This enhances the readability of the code.
3. Types of Preprocessors in C
There are basically 4 types of preprocessors in C.
Let’s take a step forward towards increasing our understanding of preprocessors by discussing each of its types in detail:
3.1 Macros
In layman language, macros are the substitutes for strings that are used while defining a constant value.
In programming terminology, a macro is a segment of code that has the ability to provide the inclusion of header files and specifies how to map a replacement output sequence in accordance to a well-defined series of steps a particular input sequence. For example,
#define MAX 100
Here, the string MAX has the assigned constant value of 100.
Also, MAX is called macro template and 100 is repressed to as macro expansion.
Key takeaway: #define macro_template macro_expression does not terminate with a semicolon.
Read more about Macros in C Language
3.2 File Inclusion
File inclusion is responsible for instructing the compiler to include a specific file in the source code program.
Depending on the type of file included, file inclusion is categorized into two types, namely:
- Standard header files – These files refer to the pre-existing files, which convey a specific meaning to the compiler before the actual compilation has taken place.
- User-defined files – The C language gives the programmer the provision to define their own header files in order to divide a complex code into small fragments.
3.3 Conditional Compilation
Just like we use if-else statements for the flow of control over specific segments of code, in the same way, we use the concept of conditional compilation. Conditional compilation is a type of preprocessor that gives the programmer the power to control the compilation of particular segments of codes. It is done with the help of the 2 popular preprocessing commands, namely:
- ifdef
- endif
3.4 Other Directives
There are 2 more preprocessor directives in C apart from the ones already discussed. They are, namely:
- #undef: As eccentric as it sounds, we use #undef directive to undefine the pre-existing, standard header or a user-defined header file.
- #pragma: We use this type of preprocessor directive to enable or disable certain features. It is important to note that #pragma varies from compiler to compiler.
4. How Preprocessor Works in C?
Since you are now well-acquainted with what preprocessors are and what they do, it’s time to discuss how they work.
In order to summarize the above discussion, here is a diagrammatic representation of how preprocessors work in C programming language:
The steps involved while preprocessing are as follows:
1. Filtering Out Comments
Since comments do not contribute to any logical statements in the program and are used only for the convenience of the user, they are disregarded during processing.
2. File Inclusion
It instructs the C compiler to include certain header files so that certain functions can be performed associated with them. It can be done in two ways:
- #include<filename.h>: The file name is enclosed between angular brackets.
- #include“filename.h”: The file name is enclosed within double-quotes.
3. Macro Expansion
There are certain situations where we want to use the same fragment of code in a recursive fashion. This is generally why we use macros instead of simple declarations or initializations.
There are 2 types of macros:
- Object-like macros: These macros are not capable of taking parameters.
- Function-like macros: These macros are capable of taking parameters.
Here is a table that summarizes the utility of preprocessors in C:
Preprocessor | Elucidation |
#include | Used to insert a specific header from a file. |
#define | Used as a replacement of a preprocessor macro. |
#ifdef |
Used when dealing with conditions. If the macro is defined, it returns true.
|
#endif |
Used to close the preprocessor directive in accordance with a given condition.
|
#undef |
Used to undefine a standard or user-defined header.
|
#pragma | Used to enable and disable certain features. |
#ifndef | If the macro is not defined, it returns true. |
#if |
Used to check if the condition is true in compile time.
|
#else |
Used to check the next condition if #if proves to be false in compile time.
|
#elif | Used as a combination of #else and #if. |
#error | Used to print error on stderr. |
Quiz on Preprocessor in C
Now, it’s time to test your learning, please attempt C preprocessor quiz
Quiz Summary
0 of 15 Questions completed
Questions:
Information
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
Results
Results
0 of 15 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
Categories
- Not categorized 0%
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- Current
- Review / Skip
- Answered
- Correct
- Incorrect
-
Question 1 of 15
1. Question
#include
#define count 1;
using namespace std;
int main()
{
int x=2;
cout<<x+count;
return 0;
}CorrectIncorrect -
Question 2 of 15
2. Question
#include
#define area(b,h) 0.5*b*h;
using namespace std;
int main()
{
int x=2,y=4;
cout<<area(x,y);
return 0;
}CorrectIncorrect -
Question 3 of 15
3. Question
#include
#define max(x,y) ((x>y)?x:y)
using namespace std;
int main()
{
int x=5,y=4;
cout<<max(x,y);
return 0;
}CorrectIncorrect -
Question 4 of 15
4. Question
#include
int add(int, int);
#define calculate
using namespace std;
int add(int x,int y)
{
#ifdef change
x=2; y=5;
cout<<x+y;
#endif
return (x+y);
}
int main()
{
int x=5,y=4;
#ifdef calculate
cout<<add(x,y);
#endif
return 0;
}CorrectIncorrect -
Question 5 of 15
5. Question
#include
int count();
#pragma stratup count
#pragma exit count
using namespace std;
int count()
{
int count=1;
cout<<count+2<<" ";
return count;
}
int main()
{
cout<<count();
return 0;
}CorrectIncorrect -
Question 6 of 15
6. Question
#include
int count();
int max(int, int);
#pragma stratup count
#pragma exit count
using namespace std;
int count()
{
int count=1;
cout<<count+2<y)?x:y);
}
int main()
{
int x=5,y=4;
cout<<max(x,y)<<" "<<count();
return 0;
}CorrectIncorrect -
Question 7 of 15
7. Question
#include
int add();
#pragma stratup add
using namespace std;
int add()
{
int x=1;
cout<<x+x<<" ";
return x;
}
int main()
{
cout<<add();
return 0;
}CorrectIncorrect -
Question 8 of 15
8. Question
#include
using namespace std;
void f1();
void f2();
#pragma stratup f1
#pragma exit f2
void f1()
{
cout<<"f1 ";
}
void f2()
{
cout<<"f2 ";
}
int main()
{
void f1();
void f2();
cout<<"main ";
return 0;
}CorrectIncorrect -
Question 9 of 15
9. Question
#include
using namespace std;
#define str1 “delhi”
#define str2 “goa”
#define str3 str1 str2int main()
{
cout<<str3;
return 0;
}CorrectIncorrect -
Question 10 of 15
10. Question
#include
using namespace std;int main()
{
cout<<__TIME__;
return 0;
}CorrectIncorrect -
Question 11 of 15
11. Question
which among the following is not a conditional compilation directive
CorrectIncorrect -
Question 12 of 15
12. Question
#pragma warn -rvl directive is used to :
CorrectIncorrect -
Question 13 of 15
13. Question
#undef directive is used to:
CorrectIncorrect -
Question 14 of 15
14. Question
__FILE__ contains:
CorrectIncorrect -
Question 15 of 15
15. Question
Which among the following a not a type of preprocessor directive?
CorrectIncorrect
Summary
Preprocessors are an important part of the C program, without them we can’t run any program. Beginners should learn this concept to master C Programming. Here, we covered details like, what are preprocessor directives, how do preprocessors work and what are its different types. We tried to understand preprocessors in C through a real-time example.