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.
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.
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.
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)
^
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.
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.
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.
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.
> 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.