What is meant by macros?how it works?what is the out put of the program#define sqr(x) x*xvoid main(){ int a,b=3;a=sqr(b+2);cout

Showing Answers 1 - 3 of 3 Answers

muthu

  • Aug 11th, 2006
 

hi friends,

    macros:

       i like to share my views regarding macros  (i.e ) macro is an abstraction that defines how a certain input pattern is replaced by an output pattern according to a defined set of rules.   they are used to automate the frequently used words in keyboard strokes or mouse movements.

 how it works: 

they are implemented as preprocessors in machines. 

the output for the above program is :

 25.

 because  the value of b goes to the equation and gets substituted and the entire value of 5 is made square root using macros and the final output is

5*5=25 gets printed

  Was this answer useful?  Yes

Harman

  • Aug 25th, 2006
 

Hi ,

The output is 11. To solve it fo not confuse yourself with scoctamic thinking.instead macro is just an alias or replacement.

so when you define sqrx(x) as x*x.then at every place where it will find sqrx(x) it will replace it with x*x.

so sqrx(B+2) will replace to (b+2*b+2) now apply bodmas and solve it

(3 +2*3+2)=(3+6+2)=11.

and mind it 25 is not correct answer.you can verify my answer

  Was this answer useful?  Yes

prema

  • Sep 12th, 2006
 

The output is 11. Macro is a preprocessor directive. During preprocessing, the preprocessor replaces every occurence of x with x*x.

In this case we have b+2 in place of x. so x*x means b+2*b+2.

here b holds the value 3.

hence it becomes (3+2*3+2)

Multiplication has got more precedence than addition. Hence multiplication takes place first and then the addition. So it's (3+6+2).

Hence the output is 11.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions