What are Preprocessor Directives in C?

This C Tutorial explains Preprocessor Directives or Symbols and their Uses in a C Program.

Every C program contains one or more functions which are prototyped in standard library header files. To use functions we need to include them in program. This inclusion is done by preprocessor directives #include. Preprocessor when operates on the source code, stumbles upon such #include directives, for ex. #include , then substitutes contents of such header files in the program at the place of respective #include directive. For ex.,

/* cprog.c */
#include <stdio.h>
 
int main(void)
{
    printf("Hello World!\n");
    return 0;
}

In above program, printf() is prototyped in stdio.h header file. Therefore, stdio.h included in program. The system library file, for ex. #include , also contains preprocessor directive EOF which marks end of each file. This is defined as,

/* 
 * End of file character.
 * Some things throughout the library rely on this being -1.
 */
#ifndef EOF
# define EOF (-1)
#endif

Preprocessor symbols all begin with ‘#’ character. Basically, these symbols include #include, #define directives, for ex.,

advertisement
advertisement
#include <stdio.h>
#include <stdlib.h>
 
#define TRUE 1
#define FALSE 0
 
#define SIZE 512
 
#define MAX(a,b)    ((a) > (b) ? (a) : (b))  /* Macro */

All preprocessor directives play textual substitution in program. #include directive includes contents of standard library file in program, #define directives, for ex.,

#define SIZE 512

replaces every occurrence of SIZE with value 512, #define macros, for ex.,

#define MAX(a,b)    ((a) > (b) ? (a) : (b))

inserts replacement text ((a) > (b) ? (a) : (b)) wherever MAX(a,b) appears in program. Besides, conditional compilation makes compiler see what fragment of code to compile and what to skip. For ex.,

advertisement
#if     constant_expression
        linux_ver1();
#elif   constant-expression
        linux_ver2();
#else
        unix_os();
#endif

Every #if construct ends with it’s matching #endif construct. Notice that constant_expression above identifies #define symbols , macros or literal constants. Any variables whose value can’t be ascertained during compile time aren’t valid candidates. Preprocessor doen’t compute the expressions. therefore, C program variables can’t be valid constant expressions.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

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

If you find any mistake above, kindly email to [email protected]

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.