0% found this document useful (0 votes)
12 views

UNIT-5-1-Preprocessor directives

The C preprocessor is a macro processor that transforms code before compilation, allowing for file inclusions, macro definitions, and conditional compilation. Key directives include #include for file inclusion, #define for defining macros, and various conditional directives like #ifdef and #if for controlling code execution based on defined macros. Macros can be object-like or function-like, with differences in type checking and execution speed, and can be undefined using #undef.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

UNIT-5-1-Preprocessor directives

The C preprocessor is a macro processor that transforms code before compilation, allowing for file inclusions, macro definitions, and conditional compilation. Key directives include #include for file inclusion, #define for defining macros, and various conditional directives like #ifdef and #if for controlling code execution based on defined macros. Macros can be object-like or function-like, with differences in type checking and execution speed, and can be undefined using #undef.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PRE PROCESSOR DIRECTIVES

The C preprocessor is a macro processor that is used automatically by the C compiler to


transform programmer defined programs before actual compilation takes place. It is called a macro
processor because it allows the user to define macros, which are short abbreviations for longer constructs.
So, it can be also be referred to as pre compiled fragments of code. Some possible actions are the
inclusions of other files in the file being compiled, definitions of symbolic constants and macros and
conditional compilation of program code and conditional execution of preprocessor directives.
List of Preprocessor directives which starts with #: #include, #define, #undef, #ifdef, #ifndef, #if, #else,
#elif, #endif, #error, #pragma

# include preprocessor directive:


Only defined at the top of the program definition and only white space and comments may appear before
preprocessor directive line. This directive includes a copy of the specified file or library. And is written
like so-
# include <filename>
or
#include “file name”,

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

Object-like Macros (Symbolic constants)


The object-like macro is an identifier that is replaced by value. It is widely used to represent
numeric constants. Used to create symbolic constants and macros, meaning useful for renaming long
named data types and certain constants or values which is being used throughout the program definition.
#define identifier replacement-text
When this line appears in a file, all subsequent occurrences of identifier that do not appear in
string literals will be replaced by the replacement text automatically before program compilation takes
place.
For example: #define PI 3.14159
Replaces all subsequent occurrences of the symbolic constant PI with numeric constant 3.14159.
Symbolic constants enable the programmer to create a name for a constant and use that name throughout
program execution. If this needs to be modified ,it has to be done in the #define directive statement. After
recompilation all definitions get modified accordingly.

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

Differences between MACRO and FUNCTION:

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:

#include<stdio.h> -- Include external source code file


#include “file1” - Include a file which is in the same folder as the parent file
#define BUFFER_SIZE 250
int maint(){
char array[BUFFER_SIZE] = [0];
int I = 9;
printf(“%d”,i);
printf(“%s”,array);
return 0;
}//end main body

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.

You might also like