What is the output of the following program?#define SQR(x) (x*x)main(){     int a,b=3;     a= SQR(b+2);     printf("%d",a);}

A) 25
B) 11
C) Error
D) Garbage Value

Showing Answers 1 - 62 of 62 Answers

vinayaka

  • Jan 14th, 2006
 

The ans is B(11).

Since it passes like (3+2) to #define, where it calculates as

(3+2 * 3+2), as Ist preference is multiply & then addition, it evalvates as

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

keerthi

  • Feb 8th, 2006
 

 ans:a

because a=sqr(5)

               a=25;

  Was this answer useful?  Yes

  • This problem is like this ex:-

    #define q(Y)(Y*Y*Y)
    main()
    {
             int a;
             int b=3;
             a=q(++b);
             printf("n a = %d b= %d",a,b);
    }

    o/p=>
    a=216
    b=6

solution:-
b=3 is taken by #define as
(++b*++b*++b)

++ > * .here ++ has higher preference than * so  it will be taken as

4*4*4=216
so a=216

and b is incremended 3 times so b=3+1+1+1=6

  Was this answer useful?  Yes

fkhanoom

  • Mar 19th, 2008
 

SQR( b + 2 ) = ( b+2 * b+2 ) = ( 3+2 * 3+2 ) Now, * has a higher precedence that +. Therefore, (3+2 * 3+2 ) = (3+6+2) = 11.

In order to get the answer 25, SQR should have be defined as
#define SQR(x) (x)*(x).
Parenthesis have a higher precedence than *.

  Was this answer useful?  Yes

mihir_void

  • Mar 5th, 2009
 

Ans is B.

The macro SQR is expanded by the pre processor as follows:

a=b+2*b+2;
the value of b being 3 the expression boils down to
a=3+2*3+2
hence a=11

  Was this answer useful?  Yes

Answer is 11. As before compilation (at the time of preprocessing) "a=SQR(b+2)"  will be like this: a = b+2*b+2;
As value of b is 3 and the precedence of * is greater than + , so it will be:
a =  3+2*3+2 which is equivalent of: a = 3+(2*3)+2 =11

  Was this answer useful?  Yes

Bhuvanesh Gupta

  • Aug 5th, 2011
 

11

  Was this answer useful?  Yes

Somasundaram

  • Aug 11th, 2011
 

the answer is "ERROR".

There is no preprocessor directive for the program... how do u think it'll execute without stdio.h???

  Was this answer useful?  Yes

shraisy

  • Aug 12th, 2011
 

11

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

  Was this answer useful?  Yes

ashutosh

  • Aug 24th, 2011
 

4*4*4=64

  Was this answer useful?  Yes

prathap

  • Aug 25th, 2011
 

garbage value

  Was this answer useful?  Yes

PASHA

  • Aug 26th, 2011
 

ans is 11

  Was this answer useful?  Yes

majumder

  • Aug 31st, 2011
 

As SQR(x) is defined as (x*x)
So, SQR(b+2)=(b+2*b+2)
=3b+2
=3*3+2
=11
So answer is 11.....As my fellow frnds has quoted..:D

  Was this answer useful?  Yes

KAVERI

  • Mar 2nd, 2016
 

D) Garbage Value

  Was this answer useful?  Yes

santhosh

  • Oct 27th, 2017
 

A) 25

  Was this answer useful?  Yes

Remember that macros are just dumb text substitutions - they dont act like functions. If you specify an arithmetic expression like "b+2" as an argument to a macro, that expression wont be *evaluated* before being operated on in the macro body; it will simply be expanded in place.

Code
  1. a = SQR(b+2); // expands to...

  2. a = (b+2 * b+2); // which, due to operator precedence, is evaluated as..

  3. a = (b + (2 * b) + 2); // and since b == 3, this evaluates as...

  4. a = (3 + (2 * 3) + 2); // which gives us...

  5. a = (3 + 6 + 2); // which yields...

  6. a = 11;

  Was this answer useful?  Yes

Ravi Raj Singhail

  • Nov 25th, 2017
 

Answer is 11, Vinayaka was right all along :/

  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