Site icon DataFlair

Learn the Importance of Preprocessors in C [Quiz included]

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:

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 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:

3.4 Other Directives

There are 2 more preprocessor directives in C apart from the ones already discussed. They are, namely:

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:

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:

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

Time limit: 0

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

Quiz complete. Results are being recorded.

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

  1. Not categorized 0%
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  1. Current
  2. Review / Skip
  3. Answered
  4. Correct
  5. Incorrect
  1. Question 1 of 15
    1. Question

    #include
    #define count 1;
    using namespace std;
    int main()
    {
    int x=2;
    cout<<x+count;
    return 0;
    }

    Correct
    Incorrect
  2. 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;
    }

    Correct
    Incorrect
  3. 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;
    }

    Correct
    Incorrect
  4. 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;
    }

    Correct
    Incorrect
  5. 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;
    }

    Correct
    Incorrect
  6. 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;
    }

    Correct
    Incorrect
  7. 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;
    }

    Correct
    Incorrect
  8. 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;
    }

    Correct
    Incorrect
  9. Question 9 of 15
    9. Question

    #include
    using namespace std;
    #define str1 “delhi”
    #define str2 “goa”
    #define str3 str1 str2

    int main()
    {
    cout<<str3;
    return 0;
    }

    Correct
    Incorrect
  10. Question 10 of 15
    10. Question

    #include
    using namespace std;

    int main()
    {
    cout<<__TIME__;
    return 0;
    }

    Correct
    Incorrect
  11. Question 11 of 15
    11. Question

    which among the following is not a conditional compilation directive

    Correct
    Incorrect
  12. Question 12 of 15
    12. Question

    #pragma warn -rvl directive is used to :

    Correct
    Incorrect
  13. Question 13 of 15
    13. Question

    #undef directive is used to:

    Correct
    Incorrect
  14. Question 14 of 15
    14. Question

    __FILE__ contains:

    Correct
    Incorrect
  15. Question 15 of 15
    15. Question

    Which among the following a not a type of preprocessor directive?

    Correct
    Incorrect

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.

Exit mobile version