Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: C Preprocessor

2 views
Skip to first unread message
Message has been deleted

Francois Grieu

unread,
Jun 30, 2010, 6:33:50 AM6/30/10
to
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

Richard Heathfield

unread,
Jun 30, 2010, 6:43:19 AM6/30/10
to
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

pete

unread,
Jun 30, 2010, 7:00:05 AM6/30/10
to
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)

Instead of doing it right?

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

I consider some things,
such as when you can get away with
deviating from good macro writing style,
to be not worth thinking about.

--
pete

Richard Heathfield

unread,
Jun 30, 2010, 7:25:58 AM6/30/10
to
pete wrote:
> 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)
>
> Instead of doing it right?

Touche'.

Ike Naar

unread,
Jun 30, 2010, 7:28:55 AM6/30/10
to
In article <aa75f96b-237f-4686...@v21g2000prd.googlegroups.com>,
sangeeta chowdhary <sangitac...@gmail.com> 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 .

Vincenzo Mercuri

unread,
Jun 30, 2010, 8:11:31 AM6/30/10
to
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

pete

unread,
Jun 30, 2010, 8:38:02 AM6/30/10
to
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

sangeeta chowdhary

unread,
Jun 30, 2010, 8:45:03 AM6/30/10
to

Thank You so much.

sangeeta chowdhary

unread,
Jun 30, 2010, 8:45:34 AM6/30/10
to
On Jun 30, 3:33 pm, Francois Grieu <fgr...@gmail.com> wrote:
> Le 30/06/2010 12:07, sangeeta chowdhary a crit :

Thank You so much.

sangeeta chowdhary

unread,
Jun 30, 2010, 8:46:16 AM6/30/10
to

Thank You so much.

sangeeta chowdhary

unread,
Jun 30, 2010, 8:47:06 AM6/30/10
to
On Jun 30, 4:28 pm, i...@localhost.claranet.nl (Ike Naar) wrote:
> In article <aa75f96b-237f-4686-90f6-b24f3c109...@v21g2000prd.googlegroups.com>,

> sangeeta chowdhary  <sangitachowdh...@gmail.com> 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.

Vincenzo Mercuri

unread,
Jun 30, 2010, 9:17:57 AM6/30/10
to
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

pete

unread,
Jun 30, 2010, 9:53:18 AM6/30/10
to
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.

--
pete

Richard Heathfield

unread,
Jun 30, 2010, 9:56:40 AM6/30/10
to
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.

Vincenzo Mercuri

unread,
Jun 30, 2010, 10:15:43 AM6/30/10
to

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

Richard Heathfield

unread,
Jun 30, 2010, 10:21:54 AM6/30/10
to
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;

Vincenzo Mercuri

unread,
Jun 30, 2010, 10:29:17 AM6/30/10
to
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

unread,
Jun 30, 2010, 10:41:54 AM6/30/10
to
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

unread,
Jun 30, 2010, 10:45:31 AM6/30/10
to
In the sense that i dont care of missing parenthesis.
Humans are supposed to be imperfect.
(sorry for my english!)

--
Vincenzo Mercuri

Kenneth Brody

unread,
Jun 30, 2010, 10:53:02 AM6/30/10
to
On 6/30/2010 6:07 AM, 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;
>
> then output is coming -25 (expected) .
>
> Guys please help me.

"Be the preprocessor" is the way to enlightenment, my son.

Expand "a=2*(s-u*t)/SQUARE(t);", and all shall be revealed.

--
Kenneth Brody

Vincenzo Mercuri

unread,
Jun 30, 2010, 6:37:55 PM6/30/10
to

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

0 new messages