Learn the Importance of Preprocessors in C | Is it worth or not?

Preprocessing 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

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.

Do you still think Is It worth learning C for 2019?

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.

What is Preprocessors in C programming Language

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-

Example of Preprocessors in C

But, what is the reason behind this error? Why is the hash/pound (#) symbol so importantIn 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:

  1. 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.
  2. 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.

Build your basic concepts with Tokens in C Programming Language

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:

Preprocessors in C with working

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:

PreprocessorElucidation
#includeUsed to insert a specific header from a file.
#defineUsed 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.
#pragmaUsed to enable and disable certain features.
#ifndefIf 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.
#elifUsed as a combination of #else and #if.
#errorUsed to print error on stderr.

Summary

Preprocessors are an important part of the C program, without it, 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.

Simplify your Long Codes using Recursive Functions in C

Have preprocessors been beneficial for you? Let us know your reviews in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.