Ads 468x60px

Wednesday, October 12, 2011

define in c Tutorial 1

# 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
01/***** All are pre processor commands *****/
02# include
03# define pi 3.14
04# undef pi
05 
06/*******************************/
07int main()
08{
09// do sum stuff here
10}
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
1# include "d:\folder1\filename.h"
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)
01# define x 123
02 
03# define y "hey"
04 
05int main(){
06 
07int i=x; //int  i=123
08 
09float j=x; // j is now 123.000000
10 
11x=5; // invalid x cannot be changed
12 
13printf(y); //
14 
15printf("%s",x);// undefined behavior
16 
17printf("%d",x);//valid
18 
19}
actually everywhere where x will appear it will be replaced by its corresponding replacement text
01# define x 123;
02 
03# define y while
04 
05int main()
06{
07 
08int i=x // valid it is equivalent to int i=123;
09 
10int a=0;
11 
12y(a<5){ //equivalent to while(a<5)
13 
14a++;
15 
16}
17 
18}
one last thing and that is very important
01# define x 10
02 
03# define y x+1 // must be # define y (x+1)
04 
05int main()
06{
07 
08int z =y*3; // equivalent to x+1*3 ==>10+1*3
09 
10printf("%d",z);// expected result 33 . Actual result 13 !
11 
12}
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)
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 
3int main(){
4 
5int area,length=5;
6 
7area = square(5); // 1st macro equivalent to area = (5*5)
8 
9}
1#define CALL(a, b) a b // macro
2int main(){
3 
4CALL(printf, ("%d %d %s\n",1, 24, "urgh")); // macro
5/* results in printf ("%d %d %s\n",1, 24, "urgh"); */
6}
you have to take care of the parenthesis. like
01# define square_incorrect(x) (x *x) // incorrect version
02 
03# define square_correct(x) ((x) *(x)) // correct version
04 
05int main(){
06 
07int a,b;
08 
09a=square_incorrect(3+7) // equivalent expression 3+7*3+7 result in 31 expected result 100 !
10 
11b=square_correct(3+7) // result in 100
12 
13}
There is something which i wanted to tell you.suppose you have a macro like
1# define f(x) f((x)+2)
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
01#define MESSAGE(x) printf("Message: %s\n", #x)
02 
03int main(){
04 
05MESSAGE (how are you);
06 
07/* above is equivalent to */
08 
09printf("Message: %s\n", "how are you")
10 
11MESSAGE (how are "you");
12 
13/* above is equivalent to */
14 
15printf("Message: %s\n", "how are \"you\"") // " or \ characters within the new string literal are preceded by \
16 
17}
hope you are fine until now.Rest we will see in next section.

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
 

Sample text

Sample Text