Home > Enterprise >  Why to put argc argv arguments in main when they never accessed?
Why to put argc argv arguments in main when they never accessed?

Time:06-18

I often see programs where people put argc and argv in main, but never make any use of these parameters.

int main(int argc, char *argv[]) {
    // never touches any of the parameters
}

Should I do so too? What is the reason for that?

CodePudding user response:

The arguments to the main function can be omitted if you do not need to use them. You can define main this way:

int main(void) {
    // never touches any of the parameters

}

Or simply:

int main() {
    // never touches any of the parameters
}

Regarding why some programmers do that, it could be to conform to local style guides, because they are used to it, or simply a side effect of their IDE's source template system.

  • Related