What is a C Preprocessor?

Question3: What is a C Preprocessor?

Answer: Preprocessor is a program that performs textual substitutions on source code even before 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>
    #include <math.h>
#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;

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 and #include into their respective places as we had typed in those contents there. Then defined symbolic constants TRUE and FALSE. Next was a piece of conditional compilation code which specified for given condition which header to be
further included and upon decision inserted it’s contents in it’s respective place into the program. Then came 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,


is substituted with 1


is substituted with 0

MAX(10, 20)

is substituted by

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

Well! How does this substitution take place? Preprocessor, firstly, examines 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.


Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

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

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!
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter