1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Who will say me the answer first?

Discussion in 'C' started by inspiration, May 24, 2010.

  1. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #define SQ(x) x*x
     main()
     {
      int a=SQ(2+1);
      printf("%d",a);
     }
    
    tell me the answer.
     
  2. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    Well the output is 5 since the macro expansion will be as follows:
    2+1*1+2
     
  3. meyup

    meyup New Member

    Joined:
    Feb 15, 2010
    Messages:
    102
    Likes Received:
    0
    Trophy Points:
    0
    I am not agree with CREATIVE the answer may be-

    It would have been 9 had you defined your micro as #define SQ(x) (x)*(x)
     
  4. gpk kishore

    gpk kishore New Member

    Joined:
    Jun 30, 2011
    Messages:
    82
    Likes Received:
    0
    Trophy Points:
    0
    I think 5
    good conceptual question from macros
     
  5. HARIT

    HARIT New Member

    Joined:
    Jul 16, 2011
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    Ans: 5 i.e. 2+1*2+1
     
  6. srp5789

    srp5789 New Member

    Joined:
    Jul 25, 2011
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    it will be 5(2+1*2+1) only.
    this is famous C MCQ question on Macros.
     
  7. ASD

    ASD New Member

    Joined:
    Aug 9, 2011
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    5 is the answer since 2+1*2+1
     
  8. ankush_urnitemare

    ankush_urnitemare New Member

    Joined:
    Mar 25, 2011
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    5 is the answer!!
     
  9. seema_26

    seema_26 New Member

    Joined:
    Sep 16, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0


    ys u r right it must be sq(x) (x)*(x)
    bt if we solve it without precedence then its answer will be 5
     
  10. seema_26

    seema_26 New Member

    Joined:
    Sep 16, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0


    ys u r right it must be sq(x) (x)*(x)
    bt if we solve it without precedence then its answer will be 5
     
  11. sainishanth

    sainishanth New Member

    Joined:
    Feb 8, 2012
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    the output will be
    as 2+1*1+2
    the higher precedence is for *operator. so
    1*1=1;
    hence=2+1+2=5;
     
  12. Fazil RF

    Fazil RF New Member

    Joined:
    Jan 7, 2013
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Erode,Tamil nadu
    Hi..
    How it ll print 5. i cant understood the concept (2+1*2+1)
    1st 1*2=2, then (2+2+1)=6
     
  13. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    Oh is it? (2+2+1)=6
     

Share This Page