# define a.k.a c preprocessor (Part 1)The term can be little bit daunting but its use is very simple.It is a separate program invoked by the compiler as the first part of translation.The preprocessor can be thought as a smart editor.It Inserts,excludes,replaces text based on commands supplied by the programmer.In this case commands are made a permanent part of the source program
for time being do not bother what these commands do.Just see how they are used in the source program.
They are many more commands .we will see them one by one.
1) # includeThe most common use of preprocessor is to include files containing declarations(header files).
#include
contains declarations of all the functions used for input output.# include will be replaced by the contents of
filename.hQues: i have seen
#include “filename.h” at many places.Is there any difference between the two?
yes there is one.
angled brackets means the preprocessor will search
filename.h in standard include directory.
in my case it will be
C:\Dev-Cpp\include.
if you have your file in any other location then you must specify the full path
for eg if location of file that i want to include is at
“d:\folder1\filename.h” then i will write
1 | # include "d:\folder1\filename.h" |
That is all for
#include2. # defineThis is also the most widely used command plus it is risky too.
it can be used in two ways
a) to define a constant (replacement macro)
b) to simulate a function (function like macro)
first lets see a)actually everywhere where x will appear it will be replaced by its corresponding replacement text
one last thing and that is very important
03 | # define y x+1 // must be # define y (x+1) |
so always use parenthesis according to your precedence
lets look at b)i.e how to simulate a function
simple protoype
1 | # define macro_name(parameter_list) (token expression) |
4 | # define square(x) (x*x) // there must be no space between -> macro_name and ( <- |
5 | # define square (x)(x*x) // it is not a function like macro.It is a replacement macro |
1 | # define square(x) (x*x) // 1st macro |
1 | #define CALL(a, b) a b // macro |
4 | CALL( printf , ( "%d %d %s\n" ,1, 24, "urgh" )); |
you have to take care of the parenthesis. like
01 | # define square_incorrect(x) (x *x) // incorrect version |
03 | # define square_correct(x) ((x) *(x)) // correct version |
09 | a=square_incorrect(3+7) |
There is something which i wanted to tell you.suppose you have a macro like
how many times this replacement is done?
if you are replacing each time then this will go into infinite replacementfor eg
a call like this
f(2)
f((2)+2)
f(((2)+2)+2)
f((((2)+2)+2)+2)…
so these are not allowed
Note if the macro name results directly from replacement text then the replacement is not done more then one levelso the call
f(2)
f((2)+2) ->will stop here.
there is one last thing in function like macro called
Stringizinglets see what it is and how it works?
There is special treatment for places in the macro replacement text where one of the macro formal parameters is found preceded by #.the # and the token list are turned into a single string literal
01 | #define MESSAGE(x) printf("Message: %s\n", #x) |
09 | printf ( "Message: %s\n" , "how are you" ) |
11 | MESSAGE (how are "you" ); |
15 | printf ( "Message: %s\n" , "how are \"you\"" ) |
hope you are fine until now.Rest we will see in next section.