Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get rid of the unused parameter warning in C with gcc 4.8.4 [-Wunused-parameter] [duplicate]

Tags:

c

gcc

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)?

like image 710
Scooter Avatar asked Nov 24 '16 00:11

Scooter


People also ask

How do I get rid of the unused variable warning?

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.

What is unused parameter?

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.

What is unused C?

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.


1 Answers

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;
}
like image 80
Weather Vane Avatar answered Oct 23 '22 11:10

Weather Vane