What does (void)var actually do?

后端 未结 4 606
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:40

      (void)argc;
      (void)argv;
    

    If a function argument is not used, like in your program, then this is the idiomatic way of suppressing the warning of unused function argument issued by some compilers. Any decent compiler will not generate code with these statements.

    It evaluates the argument but does nothing with it which has the effect for most compilers to not issue a warning. The (void) cast is used so the compiler would not produce another warning notifying that the value is not used.

    Another popular way to suppress the warning is to do:

    variable = variable;
    

    Note that I know some compilers that will issue another warning in presence of:

    (void) arg;
    

    like "statement with no effect".

提交回复
热议问题