Explains Preprocessor Directives and their Uses in C Program

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)

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

#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.,

#if     constant_expression
#elif   constant-expression

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.

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