Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

void cast of argc and argv

Tags:

c++

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?

like image 305
Mike Avatar asked Nov 08 '11 14:11

Mike


People also ask

What is the difference between argv and argc?

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.

What is void argc?

(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.

What does int argc char * argv [] mean in C C ++?

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)

Can you rename argc and argv?

Yes you can rename them as you want. They are simply function parameter names, nothing more. Save this answer.


1 Answers

"Could it be to stop the compiler from complaining about unused variables?"

yes

like image 159
IronMensan Avatar answered Sep 19 '22 20:09

IronMensan