[comp.lang.c] common mistake with comma operator.

rbutterworth@watmath.UUCP (Ray Butterworth) (01/15/87)

>"... 99%  of  all  professional C  programmers  have no idea
>what the comma operator is all about, couldn't care less and
>probably won't ever need it."

Even if this were true, they should at least be made aware of it.
Consider the following problem.  (This might be suitable for the
earlier request for examples of programming bugs.)

    #if DEBUG_ON
    #   define DEBUG(fmt,a1,a2,a3) printf(fmt,a1,a2,a3)
    #else
    #   define DEBUG(fmt,a1,a2,a3) /* nothing */
    #
    DEBUG("%d %s",17,"string");

This doesn't work because cpp requires that all 4 macro arguments
be supplied.  A common trick to get around it is to wrap all the
arguments in parentheses so that they look like a single argument
to the macro.

    #if DEBUG_ON
    #   define DEBUG(args) printf(args)
    #else
    #   define DEBUG(args) /* nothing */
    #
    DEBUG(("%d %s",17,"string"));

So now explain why the 17 is missing from the output (without
referring to the comma operator).


(P.S.  The solution is to change "printf(args)" to "printf args".)