tags:

views:

640

answers:

6

Can anyone explain what are and how to use "argc" and "argv"?

It seems my new assignment is about "argc" and "argv", but I don't know what they are.

--

thanks guys but i still dont understand what's what. these are codes from my revision notes, anyone can explain which is which that has been explained below?

class Tank{

public;

int wheels;

};

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

Tank sherman;

sherman.wheels =12;

cout << "tank has " << sherman.sheels << "wheels" >> endl;

return 0;

}
+7  A: 

These allow you access to the command line arguments that may have been specified while launching your application.

int main (int argc, char** argv)
{
    char* fileName = argv[0]; // The name of your executable

    char* firstArgument = argv[1]; // The first specified command line argument
    char* secondArgument = argv[2]; // The second specified command line argument

    int argumentCount = argc - 1;

    return 0;
}
Developer Art
Just a note, it's incorrect to use void main(...)
Alan
From the man himself: http://www.research.att.com/~bs/bs_faq2.html#void-main
Alan
Yes, it is useful to use int to return back the status code.
Developer Art
And just to be complete, if you don't add a return statement it will `return 0;` implicitly.
GMan
+7  A: 

Try doing some reading: http://www.site.uottawa.ca/~lucia/courses/2131-05/labs/Lab3/CommandLineArguments.html

Amber
This might sound rude to some people.
GMan
Not bothering to google before asking may be considered rude as well.
avakar
i am googling, just that i thought i might also get some advice from the good people here.
RealiX
Anybody who thinks not Googling before asking on this site is rude must not know the mission of this website. If it's on Google, and it's programming related, it should be on here too.
Gerald
Strangly, Dav's original answer (this is edited) was a more hand-written, nice answer. I don't think intended to sound rude.
GMan
Dav, i just realised thati have been to that site before but as stupid as i can get, i dont understand, which is why i thought i could get some layman explanation here.
RealiX
+3  A: 

When you invoke a program, you can supply command line arguments to it. E.g.:

prog-name arg1 arg2

So if you write a program and want to access command line arguments sent to it, you use argc and argv.

In the previous example, arg1 and arg2 are command line arguments. So,

argc = 3
argv[0] = "prog-name"
argv[1] = "arg1"
argv[2] = "arg2"

Real life example:

rm -r doc/

Here,

argc = 3
argv[0] = "rm"
argv[1] = "-r"
argv[2] = "doc/"

Hope that helps!

Here Be Wolves
Tiny typo: argv[0] = "rm" in the real life example.
dave
Note that there is no actual guarantee that `argv[0]` will contain the program's name: just a convention to that effect that is *usually* followed. Look at the man page for `execv` and kin to see how this expectation can be broken.
dmckee
Note that in both cases `argv[3] = NULL`. This may or may not be useful, but it's good to know.
Graeme Perrow
A: 

in C++, argc is the argument count, with the first argument always being the name of the program. argv is an array of character arrays that contain the command line arguments (argv[0] being the name of the program).

one way to use them is like so:

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

  return 0;
}

Which prints out the command line arguments and exits.

Alan
+2  A: 

When you execute your program you can give it a number of additional arguments, e.g.:

./myProgram dog cat

dog and cat will be passed into your main function. also the executable itself (./myProgram) is passed as the very first argument.

Because you don't know at the time of writing the code how many arguments you will get, your only chance is to get the arguments in a vector (array) with their count.

void main (int argc, char** argv)

argc (argument count) will hold the number of arguments including the executable name, and argv (argument vector) points to each of them. In this example:

argc = 3
argv[0] = "./myProgram"
argv[1] = "dog"
argv[2] = "cat"
Zed
+2  A: 

"argc" and "argv" are the arguments that get passed to the entry point "main()". Argument "argc" specifies the number of command line arguments (argument count) and "argv" is an array of strings that contain the actual arguments passed via command line.
For example, consider a program "testprog.exe" has an entry point like this:

int main( int argc, char** argv )
{
    for( int i = 0; i < argc; i++ )
    {
        printf( "\n argument[%d] is %s", i, argv[i] );

    }
}

If the above code is executed as:

testprog.exe hello world

Now argc would be 3, and:

  • argv[0] would contain executable name "testprog.exe"
  • argv[1] would contain "hello"
  • argv[1] would contain "world"

See here for more information:
http://publications.gbdirect.co.uk/c%5Fbook/chapter10/arguments%5Fto%5Fmain.html

swatkat