You are on page 1of 1

C Preprocessor Directives:

 Before a C program is compiled in a compiler, source code is processed by a


program called preprocessor. This process is called preprocessing.
 Commands used in preprocessor are called preprocessor directives and they
must be placed in definition section.
 All preprocessor commands begin with a pound symbol (#).
 A preprocessor directive should begin in first column.

Below is the list of preprocessor directives that C language offers.

S.no Preprocessor Syntax Description


1 Macro #define This macro defines constant value and can
be any of the basic data types.
2 Header file #include <file_name> The source code of the file “file_name” is
inclusion included in the main program .
3 Conditional #ifdef, #endif, Set of commands are included before
compilation #if, #else,  #ifndef compilation with respect to the condition
4 Other directives #undef,  #undef is used to undefine a macro

Preprocessors Examples:
Analyze the following examples to understand various directives.

e.g1: #define MAX_ARRAY_LENGTH 20


This directive tells the C to replace instances of MAX_ARRAY_LENGTH with 20.
Use #define for constants to increase readability.

e.g2: #include <stdio.h>


#include "myheader.h"
These directives tell the C to get stdio.h from System Libraries and add the text to the
current source file. The next line tells C to get myheader.h from the local directory and
add the content to the current source file.

eg3: #undef FILE_SIZE


#define FILE_SIZE 42
This tells the C to undefine existing FILE_SIZE and define it as 42.

e.g4: #ifndef MESSAGE


#define MESSAGE "You wish!"
#endif
This tells the C to define MESSAGE only if MESSAGE isn't already defined.

You might also like