A question of style
George V. Reilly
gvr at brunix
Mon Dec 4 14:33:29 AEST 1989
In article <22139 at brunix.UUCP> gvr at panda.UUCP (George V. Reilly) [I] wrote:
: Because of the two uses of the comma as a parameter separator and as a
: sequential-expression separator, you can occasionally get unexpected
: results. Consider:
:
: #define single(list) printf list
: #define double(list) printf(list)
:
: main()
: {
: single("%d %d %d", 1, 2, 3);
: double("%d %d %d", 1, 2, 3);
: }
:
: The expansion of |single()| will yield |printf("%d %d %d", 1, 2, 3)|, while
: the expansion of |double()| will yield |printf(("%d %d %d", 1, 2, 3))|.
: The argument to the second printf will be treated as four comma-separated
: expressions which evaluate to the last expression (3), ultimately
: yielding |printf(3)|, which will probably cause a segmentation fault
: and a core dump. That's what happened to me the other day, at least.
Oops, I goofed. There should have been two sets of parentheses each
for the invocations of |single()| and |double()|, thus:
single(("%d %d %d\n", 1, 2, 3));
double(("%d %d %d\n", 1, 2, 3));
Thanks to Rich Salz for pointing out that my original posting got
argument-mismatch errors from cpp.
------
George V. Reilly gvr at cs.brown.edu
uunet!brunix!gvr gvr at browncs.bitnet Box 1910, Brown U, Prov, RI 02912
More information about the Comp.lang.c
mailing list