Consider the following main()
:
int main(int argc, char *argv[])
{
return (0);
}
Upon compilation with cc -Wall -Wextra
, warnings saying "unused parameter" get generated.
When I do not need to use a parameter in a function (for instance in a signal handler function that makes no use of its int
parameter), I am used to doing the following:
int main(int argc, char *argv[])
{
(void)argc;
(void)argv;
return (0);
}
(For that particular
main()
, I sometimes see other people do:argv = argv - argc + argc
)
But what does (void)var
actually do?
I understand that (void)
is a cast, so I guess I am casting away the variable? What does the var;
line (without the cast) do? Is it an empty assignment, an empty expression?
I would like to understand what is actually going on.
expr;
is a statement with no effect. It does nothing. Writing(void) expr;
supresses the warning of a statement with no effect. Writing a statement with no effect involvingexpr
in it silences warnings involved with not usingexpr
in a function whereexpr
appears in the parameter list.