[comp.sys.mac.programmer] Think C Preprocessor Question

darweesh@zephyrus.crd.ge.com (Michael Darweesh) (10/25/90)

A simple question for those of you who know.  Probably I should have posted
to a more general C board, but here it goes.

Does the C preprocessor ever do any calculations or does it only do search
and replace?  This question is very confusing and demands an example:

#define foo 5
#define bar 8
#define baz ((foo * bar) >> 1)

integer=baz;

Basically, what I want to know is this...
Can I make the C preprocessor (specifically Think C) actually calculate
the value for baz at compile time?
I would like to be able to use this syntax to define baz as 20 instead of 
((foo * bar) >> 1).

Thanks,
-Mike Darweesh
weesh@crd.ge.com
md32@andrew.cmu.edu

rsfinn@athena.mit.edu (Russell S. Finn) (10/25/90)

In article <13065@crdgw1.crd.ge.com>, darweesh@zephyrus.crd.ge.com (Michael Darweesh) writes:
|> Does the C preprocessor ever do any calculations or does it only do search
|> and replace?  

No.  That is, it doesn't do calculations (within #define statements, anyway),
and it doesn't just search and replace -- let's look at your example:

|> #define foo 5
|> #define bar 8
|> #define baz ((foo * bar) >> 1)
|> 
|> integer = baz;

The preprocessor will replace "baz", then rescan the input and replace "foo"
and "bar" as well, so the compiler ultimately sees

   integer = ((5 * 8) >> 1);

Now, any self-respecting C compiler will perform this computation at
compile-time, which is what you probably wanted all along.  (I suspect that
this is not guaranteed by the standard, but then, I'm not Doug Gwyn...)

Russell S. Finn
rsfinn@{athena,lcs}.mit.edu

Invader@cup.portal.com (Michael K Donegan) (10/25/90)

Actually the preprocessor does do calculations, but not any that will
help you.  For example, if you said

#if baz == 20
  blah
#endif

the right thing would happen.  Calls for a built-in 'valof' operator
of some kind.
	mkd

russotto@eng.umd.edu (Matthew T. Russotto) (10/26/90)

In article <13065@crdgw1.crd.ge.com> darweesh@zephyrus.crd.ge.com (Michael Darweesh) writes:
>A simple question for those of you who know.  Probably I should have posted
>to a more general C board, but here it goes.
>
>Does the C preprocessor ever do any calculations or does it only do search
>and replace?  This question is very confusing and demands an example:
>
>#define foo 5
>#define bar 8
>#define baz ((foo * bar) >> 1)
>
>integer=baz;
>
>Basically, what I want to know is this...
>Can I make the C preprocessor (specifically Think C) actually calculate
>the value for baz at compile time?
>I would like to be able to use this syntax to define baz as 20 instead of 
>((foo * bar) >> 1).

The C preprocessor doesn't do any calculation, but the C compiler in many
'C's (including THINK, I think) will evaluate constant expressions at
compile time, giving you the same effect.
--
Matthew T. Russotto	russotto@eng.umd.edu	russotto@wam.umd.edu
Tax the rich, and feed the poor -- until there are, rich no more.