C Preprocessor
C Preprocessor is just a
text substitution tool and it instructs the compiler to do require
pre-processing before the actual compilation.
The C Preprocessor is not a part of
the compiler, but is a separate step in the compilation process.
All preprocessor commands
begin with a hash symbol (#).
#define Substitutes a preprocessor macro. |
#include Inserts a particular header from another file. |
#undef Undefines a preprocessor macro. |
#ifdef Returns true if this macro is defined. |
#ifndef Returns true if this macro is not defined. #if Tests if a compile time condition is true. |
#else The alternative for #if. |
#elif #else and #if in one statement. |
#endif Ends preprocessor conditional. |
#error Prints error message on stderr. |
Macros
Macros
are pieces of code in a program that is given some name.
Whenever
this name is encountered by the compiler, the compiler replaces the name with
the actual piece of code.
The
‘#define’ directive is used to define a macro.
Let
us now understand the macro definition with the help example of a program:
#include <stdio.h>
// macro with parameter
#define AREA(l, b) (l * b)
int main ()
{
int l1 = 10,
l2 = 5, area;
area =
AREA(l1, l2);
printf("Area
of rectangle is: %d", area);
return 0;
}
Macro substitution
Macro substitution is a
mechanism that provides a string substitution.
It can be achieved through
"#deifne".
It is used to replace the
first part with the second part of the macro definition, before the execution
of the program.
The first object may be a
function type or an object.
Parameterized Macros
In Macro if we pass parameter in macros called Parameterized
Macros.
Macros with arguments (parameters)
must be defined using the #define directive.
For example,
to square a number as follows −
int square (int x)// int x is parameter
{
return x * x;
}
#include <stdio.h>
#define MAX(x,y) ((x) > (y) ? (x): (y))
int main(void)
{
printf("Max between 20 and 10 is %d\n", MAX(10, 20));
return 0;
}
Nested Macros
A macro calling another
macro is called a nested macro.
For example
#define SQUARE(X) (X+X)
#define CUBE(X) SQUARE(X)*X
#include<stdio.h>
void main ()
{
int
a=10;
printf("\nResult of square is %d", SQUARE(a));
printf("\nResult of cube is %d",CUBE(a));
}
Difference between Macro and Function
Macro |
Function |
Macros are Preprocessed |
Functions are Compiled |
No Type Checking is done in Macro |
Type Checking is Done in Function |
Using Macro increases the code
length |
Using Function keeps the code
length unaffected |
Use of macro can lead to side
effect at later stages |
Functions do not lead to any side
effect in any case |
Speed of Execution using Macro is
Faster |
Speed of Execution using Function
is Slower |
Before Compilation, macro name is
replaced by macro value |
During function call, transfer of
control takes place |
Macros are useful when small code
is repeated many times |
Functions are useful when large
code is to be written |
Macro does not check any
Compile-Time Errors |
Function checks Compile-Time Errors |
0 comments:
Post a Comment