int main (){} not normal?

I usually just start my script like:
1
2
3
4
5
6
#include <iostream>

int main()
{
     return 0;
}


but a lot of scripts have the following:

1
2
3
4
5
6
#include <iostream>

int main(int args,char* argv[]) //Why this line of code?
{
     return 0;
}


Thank You!
This form of main int main( int argc, char*argv[] ) { /* ... */ } is used when we want to examine the command line arguments passed to our program.

Favour this form of main int main() { /* ... */ } if that is not a requirement.

See: http://en.cppreference.com/w/cpp/language/main_function
http://crasseux.com/books/ctutorial/argc-and-argv.html
Either snippet is legal.

The second snippet gives you access to the number of arguments on the command line (argc), and an array of pointers to those arguments. The first instance of the second argument (argv[0]) is always the name of the executable file.
http://www.cplusplus.com/forum/beginner/254/

BTW, the type of the second argument should be const char * argv[] although many compilers will let you get away with char * argv[]



AbstractionAnon wrote:
BTW, the type of the second argument should be const char * argv[] although many compilers will let you get away with char * argv[]
Actually, the C++ standard requires that the program be allowed to modify that data, but I always write it as
char const *const *argv anyway.
Last edited on
> the type of the second argument should be const char * argv[]

No. The strings in argv[] are modifiable (any modifications are local to the program).


> but I always write it as char const *const *argv anyway.

The IS does not guarantee that your code is portable (will be accepted by all conforming implementations).
Last edited on
Topic archived. No new replies allowed.