What does (void)var actually do?

后端 未结 4 588
Happy的楠姐
Happy的楠姐 2020-12-03 05:28

Consider the following main():

int main(int argc, char *argv[])
{
    return (0);
}

Upon compilation with cc -Wall -Wext

4条回答
  •  攒了一身酷
    2020-12-03 05:45

    As other persons correctly noted, It just suppresses a compiler warning about unused variable in your code. Btw, Win32 has defined UNREFERENCED_PARAMETER macro to reach this goal. My suggestion to make something like that in your code:

    #ifdef _WIN32
    # define UNUSED(x) UNREFERENCED_PARAMETER(x)
    #else
    # define UNUSED(x) (void) x
    #endif
    

提交回复
热议问题