Using Macros
George V. Reilly
gvr at cs.brown.edu
Thu Aug 9 09:29:30 AEST 1990
In article <17298 at haddock.ima.isc.com> karl at kelp.ima.isc.com (Karl Heuer) writes:
! % In article <642 at travis.csd.harris.com> brad at SSD.CSD.HARRIS.COM (Brad Appleton) writes:
! % Glad to see a few people finally mentioned the comma operator! Another
! % alternative along those same lines (with similar limitations) is to make
! % use of the logical operators || and &&. Using this, the following:
! %
! % #define CHECK(cond) { if (cond) exit(-1); }
! %
! % could be replaced by
! %
! % #define CHECK(cond) ((cond) || exit(-1))
! %
! Won't work if exit() is properly declared as void. Try one of
! #define CHECK(cond) ((cond) ? exit(-1) : (void)0)
! #define CHECK(cond) ((cond) || (exit(-1), 0))
!
Ah, but Karl, you missed the more important bug that Brad should have
used &&, not ||. In Brad's example, exit() will only be called if
cond == 0, which is exactly the inverse of what is desired. The same is
true of your second offering.
________________
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