I'm looking at a piece of C++ code, and the first line in the main function caught my attention:
int main(int argc, const char* argv[]) {
(void)argc; (void)argv;
...
}
Apart from this line argc and argv aren't used at all. Why is the author doing a void cast? Could it be to stop the compiler from complaining about unused variables?
The first parameter, argc (argument count) is an integer that indicates how many arguments were entered on the command line when the program was started. The second parameter, argv (argument vector), is an array of pointers to arrays of character objects.
(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.
or int main(int argc, char **argv) { /* ... */ } argc (ARGument Count) is int and stores number of command-line arguments passed by the user including the name of the program. So if we pass a value to a program, value of argc would be 2 (one for argument and one for program name)
Yes you can rename them as you want. They are simply function parameter names, nothing more. Save this answer.
"Could it be to stop the compiler from complaining about unused variables?"
yes
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