#define

Discussion in 'C Programming' started by Meenu, Jul 27, 2005.

  1. Meenu

    Meenu Guest

    the value of a is 11, how??

    #define SQR(x) (x*x)
    int main()
    {
    int a,b=3;
    a=SQR(b+2);
    printf("a=%d\n",a);
    return 0;
    }
     
    Meenu, Jul 27, 2005
    #1
    1. Advertisements

  2. Meenu

    jacob navia Guest

    Meenu a écrit :
    > the value of a is 11, how??
    >
    > #define SQR(x) (x*x)
    > int main()
    > {
    > int a,b=3;
    > a=SQR(b+2);
    > printf("a=%d\n",a);
    > return 0;
    > }
    >

    a = SQR(b+2) where SQR --> "b+2"
    a = b+2*b+2
    a = 3+(2*3)+2
    a = 3+6+2
    a = 11
     
    jacob navia, Jul 27, 2005
    #2
    1. Advertisements

  3. On 2005-07-27, Meenu wrote:
    > the value of a is 11, how??
    >
    > #define SQR(x) (x*x)


    #define SQR(x) ((x)*(x))

    Otherwise:

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

    > int main()
    > {
    > int a,b=3;
    > a=SQR(b+2);
    > printf("a=%d\n",a);
    > return 0;
    > }



    --
    Chris F.A. Johnson <http://cfaj.freeshell.org>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/cfaj/ssr.html>
     
    Chris F.A. Johnson, Jul 27, 2005
    #3
  4. Meenu wrote:
    > the value of a is 11, how??
    >
    > #define SQR(x) (x*x)
    > int main()
    > {
    > int a,b=3;
    > a=SQR(b+2);


    is
    a=b+2*b+2;
    since the value of b is 3, the value of the expansion is
    a=3+6+2;
    = 11;
    This is why, if you are _sure_ that there are no side effects in the
    arguments to SQR() it should be
    #define SQR(x) ((x)*(x))
    But, of course, you have no such guarantees about no side effects. Both
    versions die with
    SQR(++b);
    for example.
    So, what you really want is
    inline int square_int(int x) { return x*x; }





    > printf("a=%d\n",a);
    > return 0;
    > }
    >
     
    Martin Ambuhl, Jul 27, 2005
    #4
  5. Meenu

    abhik Guest

    hi meenu ,

    see the problem here is that all the #defines are taken care at
    preprocessing level .
    So wat the compiler gets is not 5*5 but actually 3 + 2 * 3 + 2
    which is 11


    Meenu wrote:
    > the value of a is 11, how??
    >
    > #define SQR(x) (x*x)
    > int main()
    > {
    > int a,b=3;
    > a=SQR(b+2);
    > printf("a=%d\n",a);
    > return 0;
    > }
     
    abhik, Jul 27, 2005
    #5
  6. "abhik" <> writes:
    > Meenu wrote:
    > > the value of a is 11, how??
    > >
    > > #define SQR(x) (x*x)
    > > int main()
    > > {
    > > int a,b=3;
    > > a=SQR(b+2);
    > > printf("a=%d\n",a);
    > > return 0;
    > > }

    >
    > see the problem here is that all the #defines are taken care at
    > preprocessing level .
    > So wat the compiler gets is not 5*5 but actually 3 + 2 * 3 + 2
    > which is 11


    Which can easily be solved by proper use of parentheses:

    #define SQR(x) ((x) * (x))

    This macro has yet another potential "bug", which is caused by using the
    macro argument twice. If the "x" expression has side-effects they will
    be doubled, yielding strange results in invocations like:

    SQR(b++, b++)

    But that's probably not what the original poster asked for :)
     
    Giorgos Keramidas, Jul 27, 2005
    #6
  7. Giorgos Keramidas wrote:

    > #define SQR(x) ((x) * (x))
    >
    > This macro has yet another potential "bug", which is caused by using the
    > macro argument twice. If the "x" expression has side-effects they will
    > be doubled, yielding strange results in invocations like:
    >
    > SQR(b++, b++)


    ....which would most likely not compile :)

    (SQR takes ONE parameter)

    Peter
     
    Peter Pichler, Jul 31, 2005
    #7
  8. Meenu

    akarl Guest

    Peter Pichler wrote:
    > Giorgos Keramidas wrote:
    >
    >> #define SQR(x) ((x) * (x))
    >>
    >> This macro has yet another potential "bug", which is caused by using the
    >> macro argument twice. If the "x" expression has side-effects they will
    >> be doubled, yielding strange results in invocations like:
    >>
    >> SQR(b++, b++)

    >
    >
    > ...which would most likely not compile :)
    >
    > (SQR takes ONE parameter)


    Moreover, even if the function *did* take two parameters it would cause
    problems (undefined behavior) even if it was a "true" function
    (modification of a variable twice with no intervening sequence point).

    August
     
    akarl, Aug 2, 2005
    #8
    1. Advertisements

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. Max

    where to define a type?

    Max, Sep 12, 2003, in forum: VHDL
    Replies:
    1
    Views:
    490
    Mike Treseler
    Sep 12, 2003
  2. Elektro
    Replies:
    1
    Views:
    582
    Egbert Molenkamp
    Feb 7, 2005
  3. theotyflos
    Replies:
    3
    Views:
    521
    Thomas Matthews
    Feb 19, 2004
  4. robin liu
    Replies:
    3
    Views:
    897
    Robin Liu
    Apr 21, 2006
  5. Brian Takita

    #define _ and #define __

    Brian Takita, Jan 23, 2006, in forum: Ruby
    Replies:
    0
    Views:
    547
    Brian Takita
    Jan 23, 2006
Loading...

Share This Page