cprogram-entry-pointargvargc

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


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?


Solution

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