views:

270

answers:

6

In many C++ IDE's and compilers, when it generates the main function for you, it looks like this:

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

When I code C++ without an IDE, just with a command line compiler, I type:

int main()

without any parameters. What does this mean, and is it vital to my program?

+20  A: 

argv and argc are how command line arguments are passed to main() in C and C++.

argc will be the number of strings pointed to by argv. This will (in practice) be 1 plus the number of arguments, as virtually all implementations will prepend the name of the program to the array.

The variables are named argc (argument count) and argv (argument vector) by convention, but they can be given any valid identifier: int main(int num_args, char** arg_strings) is equally valid.

They can also be omitted entirely, yielding int main(), if you do not intend to process command line arguments.

Try the following program:

#include <iostream>

int main(int argc, char** argv) {
    std::cout << "Have " << argc << " arguments:" << std::endl;
    for (int i = 0; i < argc; ++i) {
        std::cout << argv[i] << std::endl;
    }
}

Running it with ./test a1 b2 c3 will output

Have 4 arguments:
./test
a1
b2
c3
meagar
`argc` can be 0, in which case `argv` can be NULL. It's allowed by the standard AFAIK. I've never heard of a system that does this in practice, but it certainly could exist and wouldn't be violating any standards.
Chuck
@Chuck: Since "The value of `argv[argc]` shall be 0" (C++03 §3.6.1/2), `argv` cannot be null.
James McNellis
It's worth mentioning that `argc` stands for *"argument count"* and `argv` stands for *"argument vector"*
BlueRaja - Danny Pflughoeft
@James: Oh, good point. I was thinking of C, which has subtly different rules for `main`.
Chuck
@Chuck: C (at least C99) has the same requirement.
James McNellis
Thought I should add, this is the same in most systems out there, although they're abstracted some times. For instance, in Pascal/Delphi/Lazarus, you get; ParamStr and ParamCount (if memory serves me right). My point is, when you (if ever) write native applications in other languages/oses, there's a good chance the above is defined for you to use, and, they work perfectly the same (count/string list) in all systems which support them.
Christian Sciberras
+4  A: 

argc is the number of arguments being passed into your program from the command line and argv is the array of arguments.

you can loop through the arguments knowing the number of them like

for(int i = 0; i < argc; i++)
{
    //argv[i] is the argument at index i
}
John Boker
+3  A: 

The parameters to main represent the command line parameters provided to the program when it was started. The argc parameter represents the number of command line arguments, and char *argv[] is an array of strings (character pointers) representing the individual arguments provided on the command line.

BlueMonkMN
+2  A: 

The first parameter is the number of arguments provided and the second parameter is a list of strings representing those arguments.

Nick Gerakines
A: 

That is the way that command line arguments are read into your program. Running

$ myprogram argument1 argument2

would give you a value of 3 for argc (it counts the name of your program as 1), and [argument1, argument2] in the char*.

Andy
Your answer isn't very clear. In your example, `argc` will be 3, and `argv` will be a pointer to an array of three strings (pointers to char). `argv[0]` will usually be "myprogram" (although it may have been modified by the OS or the shell), `argv[1]` will be "argument1", and `argv[2]` will be "argument2".
Daniel Pryden
A: 

Both of

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

are legal definitions of the entry point for a C or C++ program. Stroustrup: C++ Style and Technique FAQ details some of the variations that are possible or legal for your main function.

Chris Becke