GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 169 of 453    Print  

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



  
Total Answers and Comments: 9 Last Update: March 05, 2009   
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: vinayaka
 

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.



Above answer was rated as good by the following members:
sittoop, sivaec61
January 14, 2006 02:02:53   #1  
vinayaka        

RE:

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.


 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
February 08, 2006 12:39:21   #2  
keerthi        

RE:

ans:a

because a sqr(5)

a 25;


 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 3Overall Rating: -3    
September 21, 2007 13:25:24   #3  
swati poddar Member Since: September 2007   Contribution: 2    

RE:
  • 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


 
Is this answer useful? Yes | No
October 31, 2007 18:13:06   #4  
ven Member Since: November 2005   Contribution: 10    

RE:
ANS:a 25;

SQR(x) (X*X)

x b+2 3+2 5; X 5;
SQR(x) SQR(5) SQR(5*5) 25;

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
February 20, 2008 15:48:35   #5  
sjulian Member Since: February 2008   Contribution: 2    

RE: 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);}
I go with vinayaka.
 
Is this answer useful? Yes | No
February 21, 2008 02:02:34   #6  
mouthika Member Since: February 2008   Contribution: 1    

RE: 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);}
3+2*3+2 3+6+2(since * has higher precedence)
11

 
Is this answer useful? Yes | No
February 22, 2008 03:16:14   #7  
vijoeyz Member Since: September 2007   Contribution: 3    

RE: 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);}
SQR(b+2) will be preprocessed as (3+2*3+2) which is 3+6+2 ( 11).

Thanks
Vijay Zanvar
http://faq.zanvar.in

 
Is this answer useful? Yes | No
March 19, 2008 14:13:36   #8  
fkhanoom Member Since: March 2008   Contribution: 4    

RE: 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);}
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 *.

 
Is this answer useful? Yes | No
March 05, 2009 15:09:55   #9  
mihir_void Member Since: March 2009   Contribution: 3    

RE: 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);}
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

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape