The work of pre-processor in C is to instruct the compiler and perform the required pre-processing before the actual compilation begins.
A hash (#) symbol is used before the pre-processor commands. No special character is involved in pre-processor.

The following are the pre-processor used in C programming:
1. #include – It is used at the starting of the program. It includes a header file.
2. #define – It substitutes a pre-processor macro.
3. #undef – It undefines a pre-processor macro.
4. #ifdef – This pre-processor returns true if macro is defined.
5. #ifndef – This pre-processor returns true if macro is not defined.
6. #error – It prints the error message on screen.
7. #if – Its work is to test the condition (whether it is true or not) at the compile time.
8. #else – This pre-processor is used as alternative for #if.
9. #elif – When we have to use #else and #if together i.e. in a single statement then this pre-processor is used.
10. #endif – It is used to end a pre-processor conditional statement.
11. #pragma – This pre-processor is used to issue special commands by using a standardized method.

 

Predefined Macros

C supports the following predefined macros:
_DATE_ : It shows the current date in “MMM DD YYYY” format. It is a character literal.
_TIME_ : It shows the current time in “HH : MM : SS” format. It is also treated as a character literal.
_FILE_ : It displays the current name of the file which is selected. It is a string literal.
_LINE_ : It displays the current number of the selected line. It is a decimal constant.
_STDC_ : If the compiler compiles with ANSI (American National Standards Institute) standard, it evaluates to 1.

 

Pre-processor Operator in C :

To create macros one can use following C pre-processor operators:

1. Continuation (\) Operator : Generally macros are restricted to a single line. With the help of the continuation operator you can define a macro in more than a single line.
Example :
#define message_for(r, s) \
printf( #r “and” #s “ are the students of St. Joseph \n ”)

 

2. Stringize (#) Operator: It is used to convert a macro parameter into a string constant.
Example :
#include <stdio.h>
#define message_for(r, s) \
printf( #r “and” #s “ are the students of St. Joseph. \n ”)

int main (void)
{
message_for( John, Nick);
return 0;
}

Output of the above code:
John and Nick are the students of St. Joseph.

 

3. Defined () Operator : To check if the identifier is defined using #define, if true then value is non-zero otherwise the value becomes zero.

Example :
#include <stdio.h>
If !defined (MESSAGE)
#define MESSAGE “Hello”
#endif

int main(void)
{
printf ( “The message is: %s , MESSAGE);
return 0;
}

Output of the following code:
The message is: Hello

Leave a Reply

Your email address will not be published. Required fields are marked *