confused

Discussion in 'C Programming' started by chump1708@yahoo.com, Jan 17, 2006.

  1. Guest

    #define square(a) (a*a)
    main()
    {
    printf("%d",square(4+5));
    }

    can anyone tell me why the answer is 29...I know its an inline function
    and my guess was 81...but its not...
     
    , Jan 17, 2006
    #1
    1. Advertising

  2. Guest

    Ok...got it...its bcoz
    4+5*4+4.............
     
    , Jan 17, 2006
    #2
    1. Advertising

  3. Guillaume Guest

    wrote:
    > #define square(a) (a*a)
    > main()
    > {
    > printf("%d",square(4+5));
    > }
    >
    > can anyone tell me why the answer is 29...I know its an inline function
    > and my guess was 81...but its not...


    Because the macro gets expanded as following:

    square(4+5) -> (4+5*4+5)

    Guess what, it's 29. Operators have prority.

    Typical macro definition mistake. Can you find how to modify your macro
    so as to avoid this kind of problem? Oh, and even when this mistake is
    fixed, beware of the side-effects of macro invokation...
     
    Guillaume, Jan 17, 2006
    #3
  4. Guest

    yes....here it is...
    #define square(a) ((a)*(a))
    > main()
    > {
    > printf("%d",square(4+5));
    > }
     
    , Jan 17, 2006
    #4
  5. Guest

    Ok...what are macro arguments?? n how do they differ from the normal
    macro definition??
     
    , Jan 17, 2006
    #5
  6. Xiaodong Xu Guest

    "" <> writes:

    you may define like this:

    #define square(a) ((a)*(a))

    be aware of the use of parentheses in macros definition

    > #define square(a) (a*a)
    > main()
    > {
    > printf("%d",square(4+5));
    > }
    >
    > can anyone tell me why the answer is 29...I know its an inline function
    > and my guess was 81...but its not...
     
    Xiaodong Xu, Jan 17, 2006
    #6
  7. Xiaodong Xu Guest

    "" <> writes:

    in fact I think there is no arguments for macro definition.
    it's not like function definition for it is handled by precompiler.
    the compiler just substitute the right part of macro defition for
    the left part of macro in the program before compiling.

    > Ok...what are macro arguments?? n how do they differ from the normal
    > macro definition??
     
    Xiaodong Xu, Jan 17, 2006
    #7
  8. Guillaume Guest

    wrote:
    > Ok...what are macro arguments?? n how do they differ from the normal
    > macro definition??


    Macros only define text substitution. The possibilities for side-effects
    are nearly endless.

    Typical example with your macro: square(i++)

    Not only will i be incremented twice, but the result will be undefined
    (if I'm not mistaken), because the order of evaluation in expressions
    is undefined (with some exceptions, such as logical operators).
     
    Guillaume, Jan 17, 2006
    #8
  9. wrote:
    > #define square(a) (a*a)
    > main()
    > {
    > printf("%d",square(4+5));
    > }
    >
    > can anyone tell me why the answer is 29...I know its an inline function
    > and my guess was 81...but its not...


    #define square(a) (a*a)
    square(4+5);
    expands to
    4 + 5 * 4 + 5;
    which evaluates as
    4 + 20 + 5; -> 29

    You want
    #define square(a) ((a)*(a))
     
    Martin Ambuhl, Jan 17, 2006
    #9
  10. Jordan Abel Guest

    On 2006-01-17, <> wrote:
    > #define square(a) (a*a)
    > main()
    > {
    > printf("%d",square(4+5));
    >}
    >
    > can anyone tell me why the answer is 29...I know its an inline function
    > and my guess was 81...but its not...
    >


    Because 4+5*4+5 is 29.

    try defining it as ((a)*(a))
     
    Jordan Abel, Jan 17, 2006
    #10
  11. a écrit :
    > #define square(a) (a*a)
    > main()
    > {
    > printf("%d",square(4+5));
    > }
    >
    > can anyone tell me why the answer is 29...I know its an inline function
    > and my guess was 81...but its not...
    >

    Please post complete code. See how the macro expands. As a rule of
    thumb, the parameters of a macro should be braced.

    #include <stdio.h>

    #define square(a) (a*a)

    #define square_fixed(a) ((a)*(a))

    int main(void)
    {
    printf("%d\n", square(4 + 5));
    printf("%d\n", square_fixed(4 + 5));
    return 0;
    }

    --
    A+

    Emmanuel Delahaye
     
    Emmanuel Delahaye, Jan 17, 2006
    #11
  12. Jack Klein Guest

    On 17 Jan 2006 06:27:03 -0800, ""
    <> wrote in comp.lang.c:

    > #define square(a) (a*a)
    > main()
    > {
    > printf("%d",square(4+5));
    > }
    >
    > can anyone tell me why the answer is 29...I know its an inline function


    You are wrong, it is not an inline function or any kind of function at
    all. It is a macro, and others have explained why it doesn't expand
    the way you think.

    > and my guess was 81...but its not...


    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jan 18, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DJ Miller

    Confused and bewildered.

    DJ Miller, Jul 31, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    1,083
    DJ Miller
    Sep 4, 2003
  2. RichGK
    Replies:
    1
    Views:
    398
    SStory
    Jul 31, 2004
  3. Cobra Pilot

    Confused. Need Help!

    Cobra Pilot, Jul 22, 2003, in forum: Perl
    Replies:
    1
    Views:
    972
    Shawn Corey
    Jul 22, 2003
  4. Sean Berry

    Confused on using basename...

    Sean Berry, Apr 14, 2004, in forum: Perl
    Replies:
    1
    Views:
    503
    Joe Smith
    Apr 14, 2004
  5. John
    Replies:
    0
    Views:
    278
Loading...

Share This Page