C Preprocessor

Discussion in 'C Programming' started by sangeeta chowdhary, Jun 30, 2010.

  1. Hi,
    I am trying some code and this code astonished me with its output.

    #include<stdio.h>
    #define SQUARE(x) x*x

    int main()
    {
    float s=10,u=30,t=2,a;
    a=2*(s-u*t)/SQUARE(t);
    printf("Result: %f\n",a);
    return 0;
    }

    output: -100.000000

    If I if I store result of a macro in avariable like

    x=SQUARE(t);
    a=2*(s-u*t)/x;

    then output is coming -25 (expected) .

    Guys please help me.
    sangeeta chowdhary, Jun 30, 2010
    #1
    1. Advertising

  2. Le 30/06/2010 12:07, sangeeta chowdhary a écrit :
    > Hi,
    > I am trying some code and this code astonished me with its output.
    >
    > #include<stdio.h>
    > #define SQUARE(x) x*x
    >
    > int main()
    > {
    > float s=10,u=30,t=2,a;
    > a=2*(s-u*t)/SQUARE(t);
    > printf("Result: %f\n",a);
    > return 0;
    > }
    >
    > output: -100.000000
    >
    > If I if I store result of a macro in avariable like
    >
    > x=SQUARE(t);
    > a=2*(s-u*t)/x;
    >
    > then output is coming -25 (expected) .
    >
    > Guys please help me.


    Hint: as your compiler to show the preprocessor output.

    Giveaway: you want
    #define SQUARE(x) ((x)*(x))


    Francois Grieu
    Francois Grieu, Jun 30, 2010
    #2
    1. Advertising

  3. sangeeta chowdhary

    Ike Naar Guest

    In article <>,
    sangeeta chowdhary <> wrote:
    >#define SQUARE(x) x*x
    > [...]
    > a=2*(s-u*t)/SQUARE(t);
    > [...]
    >Guys please help me.


    Have a look at the C language FAQ, http://c-faq.com/cpp/index.html .
    You asked question 10.1 .
    Ike Naar, Jun 30, 2010
    #3
  4. sangeeta chowdhary wrote:
    > Hi,
    > I am trying some code and this code astonished me with its output.
    >
    > #include<stdio.h>
    > #define SQUARE(x) x*x
    >
    > int main()
    > {
    > float s=10,u=30,t=2,a;
    > a=2*(s-u*t)/SQUARE(t);
    > printf("Result: %f\n",a);
    > return 0;
    > }
    >
    > output: -100.000000
    >
    > If I if I store result of a macro in avariable like
    >
    > x=SQUARE(t);
    > a=2*(s-u*t)/x;


    1) If /t/ is float (or double), I'd rather call pow(t,2)
    [after #include <math.h>]

    2) If /t/ is an /int/ I'd rather use SQUARE(t) [#define SQUARE(x) ((x)*(x))]
    to avoid rounding errors in pow(int, int) [this prototype doesnt exist].
    I should *never use SQUARE(t++)* though, to avoid side effects.

    --
    Vincenzo Mercuri
    Vincenzo Mercuri, Jun 30, 2010
    #4
  5. On Jun 30, 5:38 pm, pete <> wrote:
    > Vincenzo Mercuri wrote:
    >
    > > sangeeta chowdhary wrote:
    > > > #define SQUARE(x) x*x

    > > 1) If /t/ is float (or double), I'd rather call pow(t,2)
    > >     [after #include <math.h>]

    >
    > I would never write pow(t,2) instead of (t*t),
    > when (t) is the declared identifier
    > of an object with arithmetic type.
    >
    > If anybody has any trouble understanding (t*t),
    > then they can't understand pow(t,2).
    >
    > pow(t,2) is a big solution to a small problem.
    >
    > --
    > pete


    Thank You so much.
    sangeeta chowdhary, Jun 30, 2010
    #5
  6. On Jun 30, 3:33 pm, Francois Grieu <> wrote:
    > Le 30/06/2010 12:07, sangeeta chowdhary a crit :
    >
    >
    >
    >
    >
    > > Hi,
    > > I am trying some code and this code astonished me with its output.

    >
    > > #include<stdio.h>
    > > #define SQUARE(x) x*x

    >
    > > int main()
    > > {
    > >   float s=10,u=30,t=2,a;
    > >   a=2*(s-u*t)/SQUARE(t);
    > >   printf("Result: %f\n",a);
    > >   return 0;
    > > }

    >
    > > output:  -100.000000

    >
    > > If I if I store result of a macro in avariable like

    >
    > > x=SQUARE(t);
    > > a=2*(s-u*t)/x;

    >
    > > then output is coming -25 (expected) .

    >
    > > Guys please help me.

    >
    > Hint: as your compiler to show the preprocessor output.
    >
    > Giveaway: you want
    > #define SQUARE(x) ((x)*(x))
    >
    >   Francois Grieu


    Thank You so much.
    sangeeta chowdhary, Jun 30, 2010
    #6
  7. On Jun 30, 3:43 pm, Richard Heathfield <> wrote:
    > sangeeta chowdhary wrote:
    > > Hi,
    > > I am trying some code and this code astonished me with its output.

    >
    > Why?
    >
    >
    >
    > > #include<stdio.h>
    > > #define SQUARE(x) x*x

    >
    > Change this to:
    >
    > #define SQUARE(x) (x*x)
    >
    > Re-test.
    >
    > Think about the change in your results.
    >
    > It may help to consult page 53 of K&R2.
    >
    > <snip>
    >
    > --
    > Richard Heathfield <http://www.cpax.org.uk>
    > Email: -http://www. +rjh@
    > "Usenet is a strange place" - dmr 29 July 1999
    > Sig line vacant - apply within


    Thank You so much.
    sangeeta chowdhary, Jun 30, 2010
    #7
  8. On Jun 30, 4:28 pm, (Ike Naar) wrote:
    > In article <..com>,
    > sangeeta chowdhary  <> wrote:
    >
    > >#define SQUARE(x) x*x
    > > [...]
    > >  a=2*(s-u*t)/SQUARE(t);
    > > [...]
    > >Guys please help me.

    >
    > Have a look at the C language FAQ,http://c-faq.com/cpp/index.html.
    > You asked question 10.1 .


    Thank You so much.
    sangeeta chowdhary, Jun 30, 2010
    #8
  9. pete wrote:
    > Vincenzo Mercuri wrote:
    >>
    >> sangeeta chowdhary wrote:

    >
    >>> #define SQUARE(x) x*x

    >
    >> 1) If /t/ is float (or double), I'd rather call pow(t,2)
    >> [after #include<math.h>]

    >
    > I would never write pow(t,2) instead of (t*t),


    ....Richard would be happy to know that you
    appreciate his style, (t*t) instead of ((t)*(t))...
    joke aside...

    you could never do this:

    t = 1;
    while(...)
    SQUARE(t++);

    but you can do:

    t = 1;
    while(...)
    square(t++);

    with square being a function like:

    <type> square(<type> t){
    return t*t;
    }

    one for each arithmetic <type>.

    This is ok as long as your calculations dont need
    to raise a power to an exponent greater than 2 or however
    small exponents. Frankly it doesnt make any substantial
    difference.
    pow has been mainly implemented to easily handle
    cases in which the exponent is a variable on a wide range
    and when it is not of an integer type.
    That said, good #macros and no side-effects are mostly welcome

    > pow(t,2) is a big solution to a small problem.


    pow(t,2) yes, but pow is a great solution in the general case



    --
    Vincenzo Mercuri
    Vincenzo Mercuri, Jun 30, 2010
    #9
  10. pete wrote:
    > Vincenzo Mercuri wrote:
    >>
    >> pete wrote:
    >>> Vincenzo Mercuri wrote:
    >>>>
    >>>> sangeeta chowdhary wrote:
    >>>
    >>>>> #define SQUARE(x) x*x
    >>>
    >>>> 1) If /t/ is float (or double), I'd rather call pow(t,2)
    >>>> [after #include<math.h>]
    >>>
    >>> I would never write pow(t,2) instead of (t*t),

    >>
    >> ...Richard would be happy to know that you
    >> appreciate his style, (t*t) instead of ((t)*(t))...
    >> joke aside...

    >
    > I am not talking about the macro anymore.
    > Rather than write either
    >
    > SQUARE(x)
    >
    > or
    >
    > pow(t,2)
    >
    > I would write
    >
    > (t * t)
    >
    > instead.
    >
    >>> pow(t,2) is a big solution to a small problem.

    >>
    >> pow(t,2) yes, but pow is a great solution in the general case

    >
    > (t * t) works for any arithmetic type.
    > You are only recommending pow(t,2) for floating point types.
    >
    > (t * t) is portable on freestanding implementations.
    > pow(t,2) isn't portable on freestanding implementations.
    >


    comparing (t*t) to pow(t,2) doesn't make sense.
    It depends on what you have to achieve.
    If you intend to do

    t=2; and then (t*t)... i prefer 2*2

    would you like to do (t++*t++) ?

    even on a freestanding implementation it wouldnt work as you desire.






    --
    Vincenzo Mercuri
    Vincenzo Mercuri, Jun 30, 2010
    #10
  11. Richard Heathfield wrote:
    > Vincenzo Mercuri wrote:
    >> pete wrote:
    >>> Vincenzo Mercuri wrote:
    >>>>
    >>>> sangeeta chowdhary wrote:
    >>>
    >>>>> #define SQUARE(x) x*x
    >>>
    >>>> 1) If /t/ is float (or double), I'd rather call pow(t,2)
    >>>> [after #include<math.h>]
    >>>
    >>> I would never write pow(t,2) instead of (t*t),

    >>
    >> ....Richard would be happy to know that you
    >> appreciate his style, (t*t) instead of ((t)*(t))...

    >
    > No, he would be far happier if you were to appreciate the importance of
    > the extra ()s that I so carelessly omitted upthread.
    >
    > <snip>
    >


    Well... personally I appreciate new ideas way more than a fortuitous
    lack of parenthesis

    --
    Vincenzo Mercuri
    Vincenzo Mercuri, Jun 30, 2010
    #11
  12. Richard Heathfield wrote:
    > Vincenzo Mercuri wrote:
    >
    > <snip>
    >
    >> comparing (t*t) to pow(t,2) doesn't make sense.

    >
    > It sure makes sense to me.
    >
    >> It depends on what you have to achieve.
    >> If you intend to do
    >>
    >> t=2; and then (t*t)... i prefer 2*2

    >
    > I prefer 4
    >
    >> would you like to do (t++*t++) ?

    >
    > Clearly not. Write what you mean. If the intent is to assign the square
    > of t's value, incrementing t along the way, then it makes more sense to
    > do this:
    >
    > lv = t * t;
    > ++t;
    >


    Of course. I agree. I was ironically highlighting the
    difference between pow(t, 2) and (t*t). Clearly, as I said,
    there is not problem when the exponent is 2. They are almost
    interchangeable. But i don't think it would be so easy to use
    multiple products if I had to compute 2^13, 2^14, 2^6,
    with a variable exponent. At least, writing t*t*...*t
    doesnt seem to be the right path.
    --
    Vincenzo Mercuri
    Vincenzo Mercuri, Jun 30, 2010
    #12
  13. Vincenzo Mercuri wrote:
    > Richard Heathfield wrote:
    >> Vincenzo Mercuri wrote:
    >>> pete wrote:
    >>>> Vincenzo Mercuri wrote:
    >>>>>
    >>>>> sangeeta chowdhary wrote:
    >>>>
    >>>>>> #define SQUARE(x) x*x
    >>>>
    >>>>> 1) If /t/ is float (or double), I'd rather call pow(t,2)
    >>>>> [after #include<math.h>]
    >>>>
    >>>> I would never write pow(t,2) instead of (t*t),
    >>>
    >>> ....Richard would be happy to know that you
    >>> appreciate his style, (t*t) instead of ((t)*(t))...

    >>
    >> No, he would be far happier if you were to appreciate the importance of
    >> the extra ()s that I so carelessly omitted upthread.
    >>
    >> <snip>
    >>

    >
    > Well... personally I appreciate new ideas way more than a fortuitous
    > lack of parenthesis
    >

    In the sense that i dont care of missing parenthesis.
    Humans are supposed to be imperfect.
    (sorry for my english!)

    --
    Vincenzo Mercuri
    Vincenzo Mercuri, Jun 30, 2010
    #13
  14. pete wrote:
    > Vincenzo Mercuri wrote:
    >>
    >> pete wrote:
    >>> Vincenzo Mercuri wrote:
    >>>>
    >>>> sangeeta chowdhary wrote:
    >>>
    >>>>> #define SQUARE(x) x*x
    >>>
    >>>> 1) If /t/ is float (or double), I'd rather call pow(t,2)
    >>>> [after #include<math.h>]
    >>>
    >>> I would never write pow(t,2) instead of (t*t),

    >>
    >> ...Richard would be happy to know that you
    >> appreciate his style, (t*t) instead of ((t)*(t))...
    >> joke aside...

    >
    > I am not talking about the macro anymore.
    > Rather than write either
    >
    > SQUARE(x)
    >
    > or
    >
    > pow(t,2)
    >
    > I would write
    >
    > (t * t)
    >
    > instead.
    >
    >>> pow(t,2) is a big solution to a small problem.

    >>
    >> pow(t,2) yes, but pow is a great solution in the general case

    >
    > (t * t) works for any arithmetic type.
    > You are only recommending pow(t,2) for floating point types.
    >
    > (t * t) is portable on freestanding implementations.
    > pow(t,2) isn't portable on freestanding implementations.
    >


    I must admit that here I awfully explained what my point
    was. In my explanation I had in mind the case of
    pow(t,n) with n>2 . So I think I went a little OT.
    And I can't mention, maybe there isn't, any example in which
    pow(t,2) would be better than (t*t). I got later that you were
    talking about a simple expression instead of a macro, and that
    you just wanted to focus on the square of an integer.

    I don't know why I have been so obtuse to carry on my
    ideas about t^n instead of carefully reading your answers.

    Cheers

    --
    Vincenzo Mercuri
    Vincenzo Mercuri, Jun 30, 2010
    #14
    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. Chris P
    Replies:
    0
    Views:
    431
    Chris P
    Oct 28, 2003
  2. The Weiss Family

    VHDL Preprocessor

    The Weiss Family, Jul 14, 2004, in forum: VHDL
    Replies:
    2
    Views:
    3,196
    The Weiss Family
    Jul 14, 2004
  3. =?Utf-8?B?SSBhbSBTYW0=?=

    C# Preprocessor

    =?Utf-8?B?SSBhbSBTYW0=?=, Mar 13, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,566
    =?Utf-8?B?SSBhbSBTYW0=?=
    Mar 13, 2005
  4. Replies:
    0
    Views:
    2,660
  5. Cronus
    Replies:
    1
    Views:
    646
    Paul Mensonides
    Jul 15, 2004
Loading...

Share This Page