wonder about some #define tricks

Posted by kingkai on Stack Overflow See other posts from Stack Overflow or by kingkai
Published on 2010-04-25T04:27:32Z Indexed on 2010/04/25 4:33 UTC
Read the original article Hit count: 295

Filed under:
|

Whlie reading codes of my group project, I come across many DEFINEs, and some of them seems strange. To generalize it, please look at the following 2 examples.

Example 1:

#define SNPRINTF(dst, fmt, arg...)  snprintf(dst, sizeof(dst), fmt, ##arg)

what does "##" means in this circumstance? I've tried to delete both of them, and write codes like "char buf[1024]; SNPRINTF(buf,"%s,%s","abcd","efg");" which produced the same result. So "##" seems no use and no harm to me.

Example 2:

#define CLOSE(fd)   do  {   \
        if (-1 != (fd)) {   \
                    close(fd);  \
                    (fd)    = -1;   \
                }   \
} while (0)

Necessary to stuff the inner code to the do{}while(0) statement? what's the use?

Thanks!

© Stack Overflow or respective owner

Related posts about c

    Related posts about define