4

I was messing around with projects in C/C++ and I noticed this:

C++

#include <iostream.h>

int main (int argc, const char * argv[]) {
    // insert code here...
    cout << "Hello, World!\n";
    return 0;
}

and

C

#include <stdio.h>

int main (int argc, const char * argv[]) {
    // insert code here...
    printf("Hello, World!\n");
    return 0;
}

So I've always sort of wondered about this, what exactly do those default arguments do in C/C++ under int main? I know that the application will still compile without them, but what purpose do they serve?

learner
  • 1,197
  • 6
  • 22
  • 34
  • argc: argument count; argv: argument vector. They're the command line args to the program. And isn't it `char** argv` in C? – mwerschy Jun 11 '13 at 13:36
  • 3
    They provide access to the commandline arguments provided to the program. – Joe Jun 11 '13 at 13:36
  • 4
    See e.g. http://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean – Fredrik Pihl Jun 11 '13 at 13:37
  • 1
    @mwerschy: Both `char*[]` and `char**` are valid. – md5 Jun 11 '13 at 13:37
  • @Kirilenko ah ok :) Didn't know C allowed that. – mwerschy Jun 11 '13 at 13:39
  • @mwerschy http://c-faq.com/aryptr/ Arrays are basically syntactic sugar for pointers (but they aren't always exactly the same, which is why you can sometimes use `sizeof(some_array)` to get the size of the array, but not always). – JAB Jun 11 '13 at 13:41
  • For what it's worth, the `c` and `c++` standards quotes: http://pastebin.com/erHUd4Gn – BoBTFish Jun 11 '13 at 13:42
  • @BoBTFish "— argv[argc] shall be a null pointer." Huh, didn't know that. And it means you don't necessarily need `argc`, you can just iterate over `argv` until you hit the sentinel null... interesting. – JAB Jun 11 '13 at 13:44
  • `iostream.h` is not a standard header. Use `iostream`. – chris Jun 11 '13 at 15:46

4 Answers4

11

They hold the arguments passed to the program on the command line. For example, if I have program a.out and I invoke it thusly:

$ ./a.out arg1 arg2 

The contents of argv will be an array of strings containing

  1. [0] "a.out" - The executable's file name is always the first element
  2. [1] "arg1" - The other arguments
  3. [2] "arg2" - that I passed

argc holds the number of elements in argv (as in C you need another variable to know how many elements there are in an array, when passed to a function).

You can try it yourself with this simple program:


C++

#include <iostream>

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

C

#include <stdio.h>

int main(int argc, char ** argv){
    int i;
    for(i = 0; i < argc; i++){
        printf("Argument %i = %s\n", i, argv[i]);
    }
    return 0;
}
Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31
Kninnug
  • 7,992
  • 1
  • 30
  • 42
  • the argv ahs to be argv[] i think....as its an array – learner Jun 11 '13 at 13:51
  • 1
    `char ** argv` is also allowed as an array may decay into a pointer in C. I believe `char * argv[]` is indeed preferred, but I'm used to this and it saves me 1 character and is slightly faster to type. – Kninnug Jun 11 '13 at 13:55
3

If you want to accept arguments through commandline ,then you need to use the arguments in the main function .argc is the argument count and array of charecter pointers list the arguments. refer this link http://www.cprogramming.com/tutorial/c/lesson14.html

Santhosh Pai
  • 2,535
  • 8
  • 28
  • 49
2

Those are for command-line arguments. argc is the number of arguments, and the arguments are stored as an array of null-terminated strings (argv). Normally, a program with no command-line arguments passed in will still have one stored in argv; namely, the name used to execute the program (which won't always be there, depending on how the program is executed, but I can't remember what the circumstances are for that).

JAB
  • 20,783
  • 6
  • 71
  • 80
2

argc and argv 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 usually be one more than the number of arguments you pass from terminal, as usually the first is the name of the program.

Zlatomir
  • 6,964
  • 3
  • 26
  • 32