2

I am beginner in C++ and I am used to code with int main(), and now I am working with :

int main(int argc, char **argv)

And I don't know exactly what this line of code means. So, I looked up for some answer in the internet and I found this block of code:

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

Which shows me the arguments that I have.

I run the application and the console shows:

Have 1 arguments:

C:\Users\user\Documents\C++ Projects\Test\bin\Debug\Test.exe

And then it closes the application, because after this loop I have an if else statment based on the argc value.

So if argc is different from 3 it runs a exit(0).

My questions are:

Why my argument is just my own application located on debug path ?

And how can I get more than one argument ?

T.C.
  • 133,968
  • 17
  • 288
  • 421
Murilo
  • 4,453
  • 5
  • 19
  • 28

6 Answers6

11

argc is the number of arguments used to run your program

argv is an array of char* arguments

argv[0] is the name of the executable (in your case, it is Test.exe)

argv[1] is the first argument that you pass in (if you passed in any).

So if you run your program as Test.exe a b, then argc will be 3, and the contents of argv will be:

argv[0] is Test.exe

argv[1] is a

argv[2] is b

jh314
  • 27,144
  • 16
  • 62
  • 82
1

You might try to run your application in a terminal or in command line, perhaps

 .\Test.exe one two 

Then argc should be 3, with argv[0] being perhaps .\Test.exe, and argv[2] being two. I leave you to experiment what argv[1] is by using your favorite debugger.

(actually I don't know Windows, but I am transposing what I know on Linux to your proprietary Microsoft operating system)

Notice that on Linux or Posix system if you use globbing on the command line like

 /bin/ls -l a*

it is the invoking shell which expands the a* to a sequence of words, and that expanded sequence is passed by execve(2) to the /bin/ls executable.

Rumors say that on Windows that is not the case. Some Microsoft equivalent of Crt0 might do the globbing expansion. Read Microsoft documention.

AFAIK, it is guaranteed (at least on Linux and Posix systems) that argv[argc] is the NULL pointer.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

int argc, char **argv represent the command line arguments. By that I mean, if you run the application through cmd/terminal:

> mypath\test.exe a b c d
                  ^ ^ ^ ^ these arguments

argv[0] is always the path of the executable. argc is the number of arguments.

workerbee
  • 57
  • 8
yizzlez
  • 8,757
  • 4
  • 29
  • 44
1

If you are running from Visual Studio, you can add arguments in the Project Properties. Right click the project that is running, select Properties. In the Debug tab, there is a input box for Command Line Arguments. Enter them there.

For codeblocks, see here.

Community
  • 1
  • 1
clcto
  • 9,530
  • 20
  • 42
  • Another option is to create a windows shortcut and add them there. I can add some steps if that would be useful. I'm sure codeblocks has a way to add command line arguments: http://stackoverflow.com/questions/11888528/how-to-take-command-line-argument-in-codeblock-10-05 – clcto Aug 15 '14 at 20:14
  • Thanks a lot @clcto. I set the programs arguments and now it is working :) – Murilo Aug 15 '14 at 20:17
1

argc stands for argument count - argc stores in itself total number of arguments passed during runtime argv stands for argument value - argv stores in itself the arguments itself.

first argument argv[0] is the absolute path of your (executable)file

after that, every argv[n] (n < argc) will have one argument, stored as a char* string (even if you pass a number)

That'd be all.

zhirzh
  • 3,273
  • 3
  • 25
  • 30
0

The 1st argument is always the program name called, the further ones are the arguments passed on the command line.
In other words the argv replicates the complete command line called:

myprog arg1 arg2

The important thing to notice for the 1st argument, is that it can be used to distinguish certainly different behavior of the main program, depending on the program name (that can differ by e.g. using symbolic links). A good sample is how gzip and gunzip works.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190