UNIT-5-1-Preprocessor directives
UNIT-5-1-Preprocessor directives
If included inside <> then the compiler looks for the file in an implementation defined manner (or
system directory paths). If included inside a quote “ ” statement then it searches for the file in the same
directory as the program being written.
So if we include a file named file1 while writing a program name code.c which is being stored in
a folder called test. Then inclusion like so -- #include “file1” will tell the compiler to look for the file
inside the test folder, It looks at neighboring folders or subfolders too but it starts its search in the test
folder.(This sub search option is compiler dependent).
A macro is a segment of code which is replaced by the value of macro. Macro is defined by #define
directive. There are two types of macros: Object-like Macros, Function-like Macros
Function-like Macros
The function-like macro looks like function call. For example:
#define MIN(a,b) ((a)<(b)?(a):(b))
Here, MIN is the macro name.
Example for Function-like Macros :
#include <stdio.h>
#define MIN(a,b) ((a)<(b)?(a):(b))
void main() {
printf("Minimum between 10 and 20 is: %d\n", MIN(10,20));
}
Output:
Minimum between 10 and 20 is: 10
Macro Function
Macro is Preprocessed Function is Compiled
No Type Checking Type Checking is Done
Code Length Increases Code Length remains Same
Speed of Execution is Speed of Execution is Slower
Faster
Before Compilation macro name is replaced During function call , Transfer of
by macro value Control takes place
Useful where small code appears many time Useful where large code appears many
time
Generally Macros do not extend beyond one Function can be of any number of lines
line
Macro does not Check Compile Errors Function Checks Compile Errors
Preprocessor Formatting
A preprocessing directive cannot be more than one line in normal circumstances. It may be split
cosmetically with Backslash-Newline. Comments containing Newlines can also divide the directive into
multiple lines.
for example, you can split a line cosmetically with Backslash-Newline anywhere:
/*
*/ # /*
*/ defi\
ne FO\
O 10\
20
is equivalent into #define FOO 1020.
#undef
To undefine a macro means to cancel its definition. This is done with the #undef directive.
Syntax:
#undef token
define and undefine example
#include <stdio.h>
#define PI 3.1415
#undef PI
main() {
printf("%f",PI);
}
Output:
Compile Time Error: 'PI' undeclared
#ifdef
The #ifdef preprocessor directive checks if macro is defined by #define. If yes, it executes the code.
Syntax:
#ifdef MACRO
//code
#endif
#ifndef
The #ifndef preprocessor directive checks if macro is not defined by #define. If yes, it executes the code.
Syntax:
#ifndef MACRO
//code
#endif
#if
The #if preprocessor directive evaluates the expression or condition. If condition is true, it executes the
code.
Syntax:
#if expression
//code
#endif
#else
The #else preprocessor directive evaluates the expression or condition if condition of #if is false. It can be
used with #if, #elif, #ifdef and #ifndef directives.
Syntax:
#if expression
//if code
#else
//else code
#endif
Syntax with #elif
#if expression
//if code
#elif expression
//elif code
#else
//else code
#endif
Example
#include <stdio.h>
#include <conio.h>
#define NUMBER 1
void main() {
#if NUMBER==0
printf("Value of Number is: %d",NUMBER);
#else
print("Value of Number is non-zero");
#endif
getch();
}
Output
Value of Number is non-zero
Example Code:
Conditional Compilation:
Conditional compilation enables the coder to control the execution of preprocessor directives and
the compilation of program code. Conditional preprocessor directives evaluate constant integer
expressions.The ones that cannot be evaluated in preprocessor directives are sizeof expressions and
enumeration constants.
Every #if construct ends with #endif
Example –
#if !defined(MY_CONSTANT)
#define MY_CONSTANT 0
#endif
These directives determine if MY_CONSTANT is defined or not.The expression defined(MY_CONSTANT)
evaluates to 1 if MY_CONSTANT is not defined and is defined else it evaluates to 0.