Where are parameters of main() used

Pages: 12
I have heard people say that you can do the following

int main(char a,int b)

So my question is where and how these parameters are used?
it's actually:

int main(int argc, char* argv[])

These contain the space delimited command line used to execute your program. 'argc' tells how many tokens there were, and 'argv' contains the actual tokens.

IE, if your programs was invoked with:

myprogram somefile.txt -f -r

Then argc/argv would have the following values:
1
2
3
4
5
argc = 4
argv[0] = "myprogram"
argv[1] = "somefile.txt"
argv[2] = "-f"
argv[3] = "-r"




Usually when run from a GUI like Windows Explorer, people just double click the program, which results in the commandline being simply the program name. But if you "open file with" your program then the commandline is 'progname filename'... and if the user uses the command prompt to run your program, then anything is possible.
Is int argc, char *argv important?

Cant we name them differently?

BTW What is its use?

EDIT-

i tried
1
2
3
4
5
6
7
8
int main(int a,char b)
{
a5;
b=c;
cout<<a<<b;
return 0;
}

and it worked fine....

EDIT
1
2
3
4
5
6
7
int main(int a,char b)
{
a=5;
b='c';
cout<<a<<b;
return 0;
}//thanxx @TarikNeaj for the correction :D 
Last edited on
Is int argc, char *argv important?


The names can be anything, but 'argc' and 'argv' are pretty much de-facto standard and anyone familiar with C/C++ will recognize what they are and what they do.

And the types are very important.

Cant we name them differently?


Yes. But you shouldn't.

BTW What is its use?


I already explained that in my first post with examples.

i tried [snip] and it worked fine....


You lie. That code has numerous errors and would not compile in any compiler.
@Disch

http://goo.gl/ZfWrHk

See this works...
@programmer007, The link you provided looks nothing like your Snippet code.
Last edited on
concept is same :D
I typed the snippet very fast and didn't noticed... I will edit it
programmer007:

What is the point of a and b being parameters there? If you just set them to whatever you want, why not make them local variables?

EDIT:

Also, that code gives you a warning when you try to compile on the site, because main has the wrong arguments.

Try it yourself... click "compile". You get this:

sh-4.2# g++ -std=c++11 -o main *.cpp
main.cpp:5:5: warning: second argument of 'int main(int, char)' should be 'char **' [-Wmain]
 int main(int a,char b)
     ^     
Last edited on
Concepts dont compile, good code does :) Yeh other than that it works fine and it prints them out all fine, not sure if its supposed to do that, Im using VS 13.

Last edited on
@Disch

As we can assign them to the parameters that means (or at least I think that) it has a special significance.. I want to know about that..
@TarikNeaj
Concepts dont compile, good code does :)

Nice one :D

@Disch
Aside from warning it still compiles.So shouldn't it have some special meaning?
While it works, Its completely useless, You cant call main anyways, so whats the point?
Using int main(int argc, char* argv[])

does have a special significance which @Disch explained in his first post. If you dont need that just keep main parameters empty.
Last edited on
I think you need a refresher on function parameters.

A parameter is input for your function to use.

IE:

1
2
3
4
5
6
7
8
9
10
11
int add( int a, int b )  // takes a and b, adds them:
{
    return a+b;
}

// so now we can use that code like this:
int main()
{
    int foo = add(5, 6);  // takes the given 5 and 6, and adds them, giving us 11
    cout << foo; // <- prints 11 as expected
}




Now, you can throw away the value you're given as a parameter and replace it with something else, sure. But then that defeats the entire point of having a parameter at all. Example:

1
2
3
4
5
6
7
8
9
10
11
12
int add(int a, int b)
{
    a = 1;
    b = 2;
    return a + b;
}

int main()
{
    int foo  = add(5, 6);  // <- we'd expect this to add 5+6... but it doesn't.  It adds
       // 1+2.  So the 5+6 are totally worthless and are completely ignored.
}



With that in mind... the parameters from main (argc/argv) come from the OS when it runs your program. They can be used to tell your information about how your program is run.

So again I have to ask... what is the point of having parameters if you're not going to use that information and instead are just going to assign whatever you want to those values? It is completely pointless. You might as well do this:

1
2
3
4
5
int main()  // <- no need for params
{
    int a = 4;  // <- just make them local
    char b = 'c'; 
}




The whole point of main (or really any function) taking parameters is so the function can use those parameters as input.

I've already explained what input argc/argv represent and how they can be used.
Finally got it...
thanks
@Disch
@TarikNeaj

So basically parameters (excluding argv & argc) are pretty useless :D

BTW what does

my.program
somefile.text
-f
-r
signifies?
@Disch Said

Usually when run from a GUI like Windows Explorer, people just double click the program, which results in the commandline being simply the program name. But if you "open file with" your program then the commandline is 'progname filename'... and if the user uses the command prompt to run your program, then anything is possible.

Last edited on
So basically parameters (excluding argv & argc) are pretty useless :D


No!

It's the opposite! Parameters are extremely useful!!!!!!


I'm obviously not communicating clearly. I recommend reading tutorials on functions and parameters and doing a few test programs until you understand them more. I don't think there's anything I can say to clarify things.
Last edited on
@Disch I think he means for the main function only :D Although he did format that pretty poorly.
Last edited on
@Disch

Sorry, I meant arguments(if thats what they called)

@Disch & @TarikNeaj

I mean what every single element of


my.program
somefile.text
-f
-r

signifies?

EDIT- @TarikNeaj
Although he did format that pretty poorly.
That hurts :D
Last edited on
> int main(int a,char b)
> See this works...
There are two signatures for main() that compilers ought to accept
1
2
int main()
int main(int argc, char **argv)

afaik there is no restriction for a compiler to allow another prototypes.

clang rejects your code
Also, I couldn't execute the program and pass a value to 'b' ('a' was the number of arguments)


> I mean what every single element of (...) signifies?
I don't understand your question. Those are parameters that are passed to your program, it is your program the one that interprets them.



@Disch: http://dictionary.reference.com/help/faq/language/d67.html
@ne555

Yes I was just wondering that the arguments in main() are somewhat special or not...

Still I dont get the meaning of
somefile.text
-f
-r
Pages: 12