Using gcc 4.8.4 -Wall -Wextra -Wpedantic. I want to use argv for the executable name and have no need for argc.
If I do
int main(int argc, char *argv[])
I get this warning:
abc.c:5:14: warning: unused parameter ‘argc’ [-Wunused-parameter]
In the past I have just done
int main(int, char *argv[])
to get rid of the warning, but that may have been C++. Now I get an error:
abc.c:5:1: error: parameter name omitted
Is there a way to access argv and not get a warning for not accessing argc (with gcc warnings turned on)?
Solution: If variable <variable_name> or function <function_name> is not used, it can be removed. If it is only used sometimes, you can use __attribute__((unused)) . This attribute suppresses these warnings.
Reports the parameters that are considered unused in the following cases: The parameter is passed by value, and the value is not used anywhere or is overwritten immediately. The parameter is passed by reference, and the reference is not used anywhere or is overwritten immediately.
In GCC, you can label the parameter with the unused attribute. This attribute, attached to a variable, means that the variable is meant to be possibly unused. GCC will not produce a warning for this variable.
My comment was unclear:
You can write (void)argc
inside main()
in order to get rid of the compiler message without doing any harm to your program
int main(int argc, char *argv[])
{
char *prog = argv[0];
(void)argc;
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With