Why is (void) 0 a no operation in C and C++?

前端 未结 5 2137
情歌与酒
情歌与酒 2020-12-04 11:10

I have seen debug printfs in glibc which internally is defined as (void) 0, if NDEBUG is defined. Likewise the __noop for Visual C++ compiler

5条回答
  •  天涯浪人
    2020-12-04 11:28

    Even if so, why type cast it to void? Also, in case of the #define dbgprintf (void) 0, when it's called like dbgprintf("Hello World!"); -> (void) 0("Hello World!"); - what does it mean? – legends2k

    Macros replace your code with something else, so if you #defined dbgprint (that accepts x) as

    void (0)

    then no rewriting of X will occur in replacement, so dbgprintf("Helloworld") will not be converted to (void) 0("Hello world"), but to (void) 0; - not only macro name dbgprint is replaced by (void) 0, but the whole call dbgprintf("...")

提交回复
热议问题