Re: Macros
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Tue, 28 Nov 2006 21:41:22 GMT
Frederick Gotham <fgothamNO@xxxxxxxx> writes:
Harry:
# define prod(a,b)=a*b
#define PROD(a,b) ((a)*(b))
This macro will fail if either "a" or "b" contain a comma.
Only if the comma (presumably a comma operator) is at the top level of
the argument expression -- but such an expression can't be used
directly as a function argument either. You just have to parenthesize
the argument. For example:
#include <stdio.h>
#define PROD(a,b) ((a)*(b))
int main(void)
{
printf("PROD((1,2), (3,4)) = %d\n",
PROD((1,2), (3,4)));
return 0;
}
If I had written PROD(1,2, 3,4), the compiler would have no way of
knowing which comma does what.
That's why the grammar for a function call requires each argument to
be an "assignment-expression", not a more general "expression".
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- References:
- Macros
- From: Harry
- Re: Macros
- From: Frederick Gotham
- Macros
- Prev by Date: Re: Macros
- Next by Date: Re: static memory allocation versus dynamic memory allocation
- Previous by thread: Re: Macros
- Next by thread: Re: Macros
- Index(es):
Relevant Pages
|