# 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) # include
The 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.h
Ques: 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
That is all for #include
2. # define
This 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
so always use parenthesis according to your precedence
lets look at b)
i.e how to simulate a function
simple protoype
you have to take care of the parenthesis. like
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 replacement
for 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 level
so the call
f(2)
f((2)+2) ->will stop here.
there is one last thing in function like macro called Stringizing
lets 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
hope you are fine until now.Rest we will see in next section.
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
01 | /***** All are pre processor commands *****/ |
02 | # include |
03 | # define pi 3.14 |
04 | # undef pi |
05 |
06 | /*******************************/ |
07 | int main() |
08 | { |
09 | // do sum stuff here |
10 | } |
They are many more commands .we will see them one by one.
1) # include
The most common use of preprocessor is to include files containing declarations(header files).
#include
# include
Ques: 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" |
2. # define
This 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)
01 | # define x 123 |
02 |
03 | # define y "hey" |
04 |
05 | int main(){ |
06 |
07 | int i=x; //int i=123 |
08 |
09 | float j=x; // j is now 123.000000 |
10 |
11 | x=5; // invalid x cannot be changed |
12 |
13 | printf (y); // |
14 |
15 | printf ( "%s" ,x); // undefined behavior |
16 |
17 | printf ( "%d" ,x); //valid |
18 |
19 | } |
01 | # define x 123; |
02 |
03 | # define y while |
04 |
05 | int main() |
06 | { |
07 |
08 | int i=x // valid it is equivalent to int i=123; |
09 |
10 | int a=0; |
11 |
12 | y(a<5){ //equivalent to while(a<5) |
13 |
14 | a++; |
15 |
16 | } |
17 |
18 | } |
01 | # define x 10 |
02 |
03 | # define y x+1 // must be # define y (x+1) |
04 |
05 | int main() |
06 | { |
07 |
08 | int z =y*3; // equivalent to x+1*3 ==>10+1*3 |
09 |
10 | printf ( "%d" ,z); // expected result 33 . Actual result 13 ! |
11 |
12 | } |
lets look at b)
i.e how to simulate a function
simple protoype
1 | # define macro_name(parameter_list) (token expression) |
2 |
3 | //examples// |
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 |
2 |
3 | int main(){ |
4 |
5 | int area,length=5; |
6 |
7 | area = square(5); // 1st macro equivalent to area = (5*5) |
8 |
9 | } |
1 | #define CALL(a, b) a b // macro |
2 | int main(){ |
3 |
4 | CALL( printf , ( "%d %d %s\n" ,1, 24, "urgh" )); // macro |
5 | /* results in printf ("%d %d %s\n",1, 24, "urgh"); */ |
6 | } |
01 | # define square_incorrect(x) (x *x) // incorrect version |
02 |
03 | # define square_correct(x) ((x) *(x)) // correct version |
04 |
05 | int main(){ |
06 |
07 | int a,b; |
08 |
09 | a=square_incorrect(3+7) // equivalent expression 3+7*3+7 result in 31 expected result 100 ! |
10 |
11 | b=square_correct(3+7) // result in 100 |
12 |
13 | } |
1 | # define f(x) f((x)+2) |
if you are replacing each time then this will go into infinite replacement
for 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 level
so the call
f(2)
f((2)+2) ->will stop here.
there is one last thing in function like macro called Stringizing
lets 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) |
02 |
03 | int main(){ |
04 |
05 | MESSAGE (how are you); |
06 |
07 | /* above is equivalent to */ |
08 |
09 | printf ( "Message: %s\n" , "how are you" ) |
10 |
11 | MESSAGE (how are "you" ); |
12 |
13 | /* above is equivalent to */ |
14 |
15 | printf ( "Message: %s\n" , "how are \"you\"" ) // " or \ characters within the new string literal are preceded by \ |
16 |
17 | } |
0 comments:
Post a Comment