INPUT ARGUMENT IN MAIN FUNCTION

Oct 23, 2012 at 9:33am
Could anyone please explain me easily what
exactly the main function input arguments, in
both ways,

1
2
int main(int argc, char* argv[]) {
int main(int argc, char** argv) {


do?
Oct 23, 2012 at 9:46am
both signatures are the same

argc is the number of arguments passed to the program, from command line
argc can have values >= 1, first argument is always the name of the program itself.

argv is the array of command line arguments passed to the program. argv[0] is always the name of the program. argv[1] onwards are the actual parameters passed

example
prompt> testprog argu argument


In this case the argc = 3 argv[0] = testprog argv[1] = argu argv[2] = argument
Oct 23, 2012 at 11:08am
so:

- I am gonna need these arguments only to launch the program from the command line

- If I don´t plan to launch the code from command line I can write the main function without any arguments, like

int main() {...

right?
Oct 23, 2012 at 11:11am
You may define main either as having two parameters or having no parameters or in implementation-defined manner.
If you do not need parameters then you may define main without them.
Oct 23, 2012 at 11:58am
thanks a lot to both of you!
Oct 23, 2012 at 12:33pm
Only take into account that the following statement of @codewalker

argc can have values >= 1, first argument is always the name of the program itself.


is invalid. argc can be equal to zero.
Topic archived. No new replies allowed.