in Programming in C edited by
446 views
1 vote
1 vote
Consider the following program:
# define Rec(a) a + a * a
int a;
a = 20 + Rec(a) * Rec(a + 1);
printf(“%d”, a)
return 0;
}
The output of above program for a = 3 is ________.
in Programming in C edited by
446 views

2 Answers

7 votes
7 votes
Best answer

#define is a Macro. What is the meaning of Macro is that, it will replace the value at compile time. It means that 

If we have defined 

#define FIVE 5

then in this for loop,

for(int i=0; i<FIVE; i++)

When compiler compile the above line then it will replace FIVE with 5. The point I want to make here that Program replaces Macro before starting the execution of the program.
Now comes to Question. 
#define Rec(a) a + a * a

At compile time expression a = 20 + Rec(a) * Rec(a + 1); will be replaced as
a = 20 + a + a * a * a + 1 + a + 1 * a + 1;  (lets evaluate it)
a = 20 + 3 + 27 + 1 + 3 + 3 + 1;
a = 58. 


Hence Program will print 58.
 

selected by
by

1 comment

Macro just go on substituting and then you can correctly eval to 58
0
0
2 votes
2 votes

Rec(a)=a+a*a 

Rec(a+1)=a+1+a+1*a+1
After compilation this line a = 20 + Rec(a) * Rec(a + 1); become convert into this:-
                                                   a=20+a+a*a  *  a+1+a+1*a+1
for a=3
value of a will be printed 58.

 

Related questions