What is C Preprocessor?

This tutorial explains what is C Preprocessor with examples.

Question: What is a C Preprocessor?

Answer: Preprocessor is a program that performs textual substitutions on source code even before the program is compiled. Basically, this deletes the comments, inserts the code of specified #included headers into the program, defines and substitutes the #define symbols and macros and sets, using conditional compilation, which fragment of code to be processed or skipped by the compiler.

Let’s take an example:

/* 
 * cpp_functions.c -- program shows different functions performed by C  
 * pre-preprocessor
 */
#include <stdio.h>
#include <stdlib.h>
 
/* TRUE and FALSE are symbolic constants */
#define TRUE 1
#define FALSE 0
 
/* conditional compilation */
#if (TRUE)
    #include <string.h>
#elif (!FALSE)
    #include <ctype.h>
#else
    #include <math.h>
#endif
 
#define MAX(a,b) ((a) > (b) ? (a) : (b)) /* MAX is a macro */
 
 
int main(void)
{
    int x = 10, y = 20;
    float u = 12.34, v = -0.98;
    double s = 113.563, t = 34.65;
 
    /* Let's use Macro MAX(a,b) to compare two values */
    printf("The greater of %d and %d is: %d \n", x, y, MAX(x, y));
    printf("The greater of %f and %f is: %.2f\n", u, v, MAX(u, v));
    printf("The greater of %lf and %lf is: %.6lf\n", s, t, MAX(s, t));
 
    return 0;
 
}

Program Explanation:

advertisement
advertisement
  • In the above program doesn’t do any useful. Nevertheless, it used preprocessor directives #include, #define which cause preprocessor to define and substitute them into program before being compiled.
  • Firstly, it inserts the contents of headers #include <stdio.h> and #include <stdlib.h> into their respective places as we had typed in those contents there.
  • Then defined symbolic constants TRUE and FALSE.
  • Following that was a piece of conditional compilation code that specified for each given condition which header should be included further and, upon decision, inserted its contents into the program.
  • Then there was a defined macro called MAX(a,b).
  • In main() we used MAX() to compare values.
  • Preprocessor performs textual substitutions on the source code. Wherever it found symbolic constants substituted them with their respective definitions.

For example,

TRUE

is substituted with 1

FALSE

is substituted with 0

MAX(10, 20)

is substituted by

advertisement
((10) > (20) ? (10) : (20));

How does this substitution take place using Preprocessor?
First, the preprocessor checks macro arguments to see if they contain any #defined symbols. And then sunstitution text is inserted in place of original text into the program. For macros, Argument names are replaced with their values. Code is scanned again to see if there are any #defined symbols. If so, process is repeated again and again.

advertisement

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.