Re: Macros



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



Relevant Pages

  • Re: whats wrong with this code from a book?
    ... you need to enclose them in curly braces. ... > What you've done here is use the comma operator to combine the ... > There are rules about the value yielded by an expression with a comma ... exchange of elements in 'reverse', where the exchange can be thought of ...
    (comp.lang.c)
  • Re: whats the meaning of the following expression?
    ... >>The comma operator is used for function arguments. ... argument lists. ... I read the first paragraph then the examples, ...
    (microsoft.public.vc.language)
  • Re: Order of evaluation of function arguments
    ... > Combine this with the common misconception that the commas separating ... > arguments are comma operators, ... Note that, although the comma operator is a sequence point, the ... Of course, to confuse things, one could combine the comma operator ...
    (comp.lang.c)
  • Re: printf output
    ... The comma operator does guarantee left-to-right evaluation, ...
    (comp.lang.c)
  • Re: Order of evaluation.
    ... >> The comma in a parameter list is not the comma operator. ... but I'd presume that commas in argument lists ... Comeau C/C++ with Dinkumware's Libraries... ...
    (alt.comp.lang.learn.c-cpp)