C Programming Tutorial Part 2 - Preprocessors

On this page

  1. Conclusion

In the first part of our ongoing C programming tutorial series, we briefly touched on the preprocessing stage. In this tutorial, we will discuss it in a little more detail so that you have a basic idea about it before learning other C programming aspects.

Preprocessing is usually the first stage of program compilation, where-in anything beginning with a '#' gets processed. Let's take a basic example code.

#include <stdio.h>

int main (void)
{
    printf("\n Hello World \n");
    return 0;
}

Yes, this is the same piece of code we used in the first part of this article series. Here, it's the first line that begins with a hash '#'. In layman's terms, the line '#include <stdio.h>' makes sure the content of header file 'stdio.h' are included in this program during compilation.

So what happens in the preprocessing stage is, this line gets replaced with the actual content of the header file. Here's how the code looks after the preprocessing stage:

C Code File

So you can see, everything above the few lines of code we wrote (look at the bottom of the screenshot) is the contents of stdio.h. Now, I won't go in details of how I managed to see the code after preprocessing, but this was to give you a clear idea on what happens during preprocessing.

Moving on, lines that include a header file aren't the only ones that begin with a hash (#). There's also something called Macros that also begin with #. So it's important to discuss them here (at least their basics).

A Macro is nothing but a name (usually in capital letters) that refers to a piece of code. Macros are defined using the '#define' directive. Here's an example:

#include <stdio.h>
#define PRINT_HELLO_WORLD printf("\n Hello World \n");
int main (void)
{
    PRINT_HELLO_WORLD
PRINT_HELLO_WORLD
    return 0;
}

So we defined a macro PRINT_HELLO_WORLD with value printf("\n Hello World \n"); . Now, wherever we'll use this macro name in the code, it will get replaced with its value during the preprocessing stage. Here's the code after pre-processing:

C Program Macro

So you see that both occurrences of the macro were replaced by its value at the pre-processing stage. Of course, there are several other details related to macros that you should know about in order to successfully use them. We will discuss those details in a Macro dedicated part of this tutorial series.

Moving on, the third type of lines beginning with # are #ifdef and #endif. These are used for conditional compilation. To give you a quick idea, there are times when you'd not want a part of code to get compiled based on a certain condition, it's in cases like these, these directives are used.

Here's an example:

#include <stdio.h>
#define PRINT_HELLO_WORLD printf("\n Hello World \n");

int main (void)
{
#ifdef CONDITION
    PRINT_HELLO_WORLD
#endif
    PRINT_HELLO_WORLD
    return 0;
}

In the code above, since 'CONDITION' isn't defined anywhere, so the first occurrence of 'PRINT_HELLO_WORLD' will be omitted from code at the preprocessing stage only. Here's the proof:

Code condition

So you can see only one printf statement after the preprocessing stage.

Conclusion

In this tutorial, we discussed preprocessing to give C programming beginners a basic idea of what happens during this stage. Now, move on to part 3 to learn further about C programming basics.

Share this page:

Suggested articles

0 Comment(s)

Add comment