Preprocessor Preprocessor: List of Preprocessor Commands
Preprocessor Preprocessor: List of Preprocessor Commands
Preprocessor
Preprocessor directive is an instruction given to the compiler before the execution of actual
program.
Preprocessor directive are processed by a program known as preprocessor.
Preprocessor is a program which perform before the compilation.
It is placed before the main().
It begins with a # symbol.
They are never terminated with a semicolon.
List of Preprocessor commands:
1. #include
2. #define
3. #undef
4. #if -#else-#endif
5. #elif
6. #ifdef
7. #ifndef
The #include preprocessor directive is used to include another source file in our source code. This
is commonly used to include header files in our program.
#include "filename.h"
or
#include <filename.h>
The source file to be added must be enclosed within double quotes (" ") or angle brackets (< >) . If
the file is enclosed in double quotes the search will be done in current directory. If the file is
enclosed in angle brackets the search will be done in standard directories (include directory)
where the libraries are stored.
1
Example:
#include <stdio.h>
#include "main.h"
#include "win/display.c"
Here, file "stdio.h" is a standard header file, "main.h" and "win/display.c" is custom C files.
Example :
#define PI 3.142
#define HUNDRED 100
2
PPS NOTES_UNIT-III_CH SUBBA REDDY
Macros using #define :
#include <stdio.h>
// Macro definition
#define TRUE 1
#define FALSE 0
int main()
{
printf("TRUE: %d\n", TRUE);
printf("FALSE: %d\n", FALSE);
return 0;
}
The #define Preprocessor Directive as Function
The #define preprocessor directive can be used as inline function which is replaced at compile
time. The definition of macro accepts an argument and that argument is replaced by the actual
argument found in program.
Syntax :
Output :
Enter any number : 5
The square is : 25
3
PPS NOTES_UNIT-III_CH SUBBA REDDY
Predefined Macros :
ANSI C defines a number of macros. Although each one is available for use in programming, the
predefined macros should not be directly modified.
__DATE__ : The current date as a character literal in "MMM DD YYYY" format.
__TIME__ : The current time as a character literal in "HH:MM:SS" format.
__FILE__ : This contains the current filename as a string literal.
__LINE__ : This contains the current line number as a decimal constant.
__STDC__ : Defined as 1 when the compiler complies with the ANSI standard.
Example :
#include <stdio.h>
int main()
{
printf("File :%s\n", FILE );
printf("Date :%s\n", DATE__ );
printf("Time :%s\n", TIME );
printf("Line :%d\n", __LINE__ );
printf("ANSI :%d\n", STDC );
}
3 : #undef preprocessor directive
We use #undef directive to remove a defined macro. We generally un-define a macro, when we do
not require or want to redefine it. To redefine a macro, we need to undefined the macro and
redefine it.
Syntax:
#undef MACRO_NAME
4
PPS NOTES_UNIT-III_CH SUBBA REDDY
Example:
#include <stdio.h>
#define TRUE 1
#define FALSE 0
int main()
{
printf("TRUE: %d\n", TRUE);
printf("FALSE: %d\n", FALSE);
// Undefine a previously defined macro
#undef TRUE
#undef FALSE
// Re-define macro values
#define TRUE 0
#define FALSE 1
printf("\nMacro values are redefinition\n");
printf("TRUE: %d\n", TRUE);
printf("FALSE: %d\n", FALSE);
return 0;
}
4 : The #if-#else-#endif Preprocessor Directive
The #if preprocessor directive takes condition in parenthesis, if condition is true, then the
statements given between #if and #else will get execute. If condition is false, then statements
given between #else and #endif will get execute.
Syntax of #if-#else-#endif Preprocessor Directive
#if(condition)
----------
----------
#else
----------
----------
#endif
5
PPS NOTES_UNIT-III_CH SUBBA REDDY
Example of #if-#else-#endif Preprocessor Directive
#include<stdio.h>
#define MAX 45
void main()
{
#if MAX > 40
printf("Yes, MAX is greater then 40.");
#else
printf("No, MAX is not greater then 40.");
#endif
}
The #elif preprocessor directive is similar to if-else ladder statement. It is used for checking
multiple conditions, if the first condition will not satisfy, compiler will jump to #else block and
check other condition is true or not and so on.
#if(condition)
----------
----------
#elif(condition)
----------
----------
#elif(condition)
----------
----------
#else
----------
----------
#endif
6
PPS NOTES_UNIT-III_CH SUBBA REDDY
Example of #if-#elif-#else-#endif Preprocessor Directive
#include<stdio.h>
#define MKS 65
void main()
{
#if MKS>=78
printf("\nGrade A");
#elif MKS>=50
printf("\nGrade B");
#elif MKS>=25
printf("\nGrade C");
#else
printf("\nGrade D");
#endif
The #ifdef preprocessor directive is used to check whether the macro-name is previously defined
or not, if defined then the statements given between #ifdef and #else will get execute.
#ifdef macro-name
----------
----------
#else
----------
----------
#endif
7
PPS NOTES_UNIT-III_CH SUBBA REDDY
Example of #ifdef preprocessor directive
#include<stdio.h>
#define MKS 65
void main()
{
#ifdef MONTH
printf("\nMONTH is defined.");
#else
printf("\nMONTH is not defined.");
#endif
}
The #ifndef preprocessor directive is used to check whether the macro-name is previously defined
or not, if not defined then the statements given between #ifndef and #else will get execute.
Syntax of #ifndef preprocessor directive
#ifndef macro-name
----------
----------
#else
----------
----------
#endif
8
PPS NOTES_UNIT-III_CH SUBBA REDDY