702

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?

1
  • 65
    If your program is going to ignore command line arguments, then what you write is fine. If your program needs to process command line arguments, then the IDE is doing it right. Jun 11, 2010 at 15:57

11 Answers 11

879

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:\n";
    for (int i = 0; i < argc; ++i) {
        std::cout << argv[i] << "\n";
    }
}

Running it with ./test a1 b2 c3 will output

Have 4 arguments:
./test
a1
b2
c3
4
  • 3
    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
    Oct 8, 2010 at 15:59
  • But then again, if argc is 0 and argv NULL, then surely argv[argc] = *(NULL + 0) = *NULL = NULL = 0, right? Jan 11, 2014 at 21:11
  • 13
    @EmilVikström No, that's a serious error that probably results in a segfault. *NULL is definitely not equal to NULL.
    – user229044
    Jan 12, 2014 at 1:42
  • @EmilVikström You could do for (char **arg = argv; *arg; arg++) { printf("\t%s\n", *arg);} The *arg in the for loop insinuates *arg != NULL I believe (i.e. while *arg is true).
    – dylnmc
    Sep 25, 2014 at 16:20
66

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
}
0
29

Suppose you run your program thus (using sh syntax):

myprog arg1 arg2 'arg 3'

If you declared your main as int main(int argc, char *argv[]), then (in most environments), your main() will be called as if like:

p = { "myprog", "arg1", "arg2", "arg 3", NULL };
exit(main(4, p));

However, if you declared your main as int main(), it will be called something like

exit(main());

and you don't get the arguments passed.

Two additional things to note:

  1. These are the only two standard-mandated signatures for main. If a particular platform accepts extra arguments or a different return type, then that's an extension and should not be relied upon in a portable program.
  2. *argv[] and **argv are exactly equivalent, so you can write int main(int argc, char *argv[]) as int main(int argc, char **argv).
3
  • 1
    Thanks for the nice pedantry @Justin. Answer updated to be more correct. Jan 23, 2017 at 12:34
  • Is the last element of argv always NULL? I'm reading some code where argv[1] is called without checking if argc > 1, and the person who wrote the code clearly expected to either have a correct value, or NULL.
    – user276648
    Jun 20, 2022 at 3:39
  • 2
    @user276648, yes - The value of argv[argc] shall be 0. Jun 20, 2022 at 7:37
18
int main();

This is a simple declaration. It cannot take any command line arguments.

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

This declaration is used when your program must take command-line arguments. When run like such:

myprogram arg1 arg2 arg3

argc, or Argument Count, will be set to 4 (four arguments), and argv, or Argument Vectors, will be populated with string pointers to "myprogram", "arg1", "arg2", and "arg3". The program invocation (myprogram) is included in the arguments!

Alternatively, you could use:

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

This is also valid.

There is another parameter you can add:

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

The envp parameter also contains environment variables. Each entry follows this format:

VARIABLENAME=VariableValue

like this:

SHELL=/bin/bash    

The environment variables list is null-terminated.

IMPORTANT: DO NOT use any argv or envp values directly in calls to system()! This is a huge security hole as malicious users could set environment variables to command-line commands and (potentially) cause massive damage. In general, just don't use system(). There is almost always a better solution implemented through C libraries.

14

Lets consider the declaration:

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

In the above declaration, the type of the second parameter named argv is actually a char**. That is, argv is a pointer to a pointer to a char. This is because a char* [] decays to a char** due to type decay. For example, the below given declarations are equivalent:

int main (int argc, char *argv[]); //first declaration
int main (int argc, char **argv);  //RE-DECLARATION. Equivalent to the above declaration

In other words, argv is a pointer that points to the first element of an array with elements of type char*. Moreover, each elements argv[i] of the array(with elements of type char*) itself point to a character which is the start of a null terminated character string. That is, each element argv[i] points to the first element of an array with elements of type char(and not const char). A diagram is given for illustration purposes:

argv and argc

As already said in other answers, this form of declaration of main is used when we want to make use of the command line argument(s).

1
  • 1
    The diagram looks good! Which software did you use?
    – kfb
    Jan 2 at 9:14
11

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.

2
  • 3
    Argv[] always has argv[arg] as a null pointer. and Argv[0] is always the (full path)/executableName as a nul terminated string Dec 21, 2014 at 8:09
  • 3
    @user3629249: Not necessarily; argv[0] is whatever the the program launching the C program gave it as argv[0]. In the case of Bash, it is often (maybe always) the pathname of the executable, but Bash is not the only program that executes other programs. It is permissisble, though eccentric, to use: char *args[] = { "cat", "/dev/null", "/etc/passwd", 0 }; execv("/bin/ls", args);. On many systems, the value seen by the program as argv[0] will be cat, even though the executable is /bin/ls. Feb 5, 2016 at 3:36
8

The main function can have two parameters, argc and argv. argc is an integer (int) parameter, and it is the number of arguments passed to the program.

The program name is always the first argument, so there will be at least one argument to a program and the minimum value of argc will be one. But if a program has itself two arguments the value of argc will be three.

Parameter argv points to a string array and is called the argument vector. It is a one dimensional string array of function arguments.

0
5

Command-line Arguments: main( int argc, char * argv[] )

In Unix, when you pass additional arguments to a command, those commands must be passed to the executing process. For example, in calling ls -al, it executes the program ls and passes the string -al as an argument:

% ls
content.html  index.html  primary.0.html
% ls -al
total 20
drwxr-xr-x 2 dwharder users 4096 Sep 11 16:38 .
drwxr-xr-x 6 dwharder users 4096 Sep 11 16:35 ..
-rwxr-xr-x 1 dwharder users  117 Sep 11 16:38 content.html
-rwxr-xr-x 1 dwharder users 1400 Sep 11 16:37 index.html
-rwxr-xr-x 1 dwharder users  532 Sep 11 16:38 primary.0.html
%

The way a running program accesses these additional parameters is that these are passed as parameters to the function main:

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

Here argc means argument count and argv means argument vector.

The first argument is the number of parameters passed plus one to include the name of the program that was executed to get those process running. Thus, argc is always greater than zero and argv[0] is the name of the executable (including the path) that was run to begin this process. For example, if we run

#include <stdio.h>

int main( int argc, char *argv[] ) {
        printf( "argv[0]:  %s\n", argv[0] );

        return 0;
}

Here we compile this code and first compile and run it so that the executable name is a.out and then we compile it again and run it so the executable name is arg:

% gcc argument.0.c
% ./a.out 
argv[0]:  ./a.out
% gcc -o arg argument.0.c
% ./arg 
argv[0]:  ./arg
%

If more additional command-line arguments are passed, the string of all characters is parsed and separated into substrings based on a few rules; however, if all the characters are either characters, numbers or spaces, the shell will separate the based on spaces and assign args[1] the address of the first, args[2] the address of the second, and so on.

The following program prints all the arguments:

#include <stdio.h>

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

        printf( "argc:     %d\n", argc );
        printf( "argv[0]:  %s\n", argv[0] );

        if ( argc == 1 ) {
                printf( "No arguments were passed.\n" );
        } else {
                printf( "Arguments:\n" );

                for ( i = 1; i < argc; ++i ) {
                        printf( "  %d. %s\n", i, argv[i] );
                }
        }

        return 0;
}

Here we execute this program with one and then twelve command-line arguments:

% gcc argument.c
% ./a.out first
argc:     2
argv[0]:  ./a.out
Arguments:
  1. first
% ./a.out first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth
argc:     13
argv[0]:  ./a.out
Arguments:
  1. first
  2. second
  3. third
  4. fourth
  5. fifth
  6. sixth
  7. seventh
  8. eighth
  9. ninth
  10. tenth
  11. eleventh
  12. twelfth
% 

For Fun

Now, you may be wondering what actually happens (and if you don't, you're welcome to skip this). First, when the command is executed, the shell parses the command line and separates the individual pieces by a null character \0. For example, the execution of

% ./a.out first second third fourth fifth

has the shell generate the string ./a.out☐first☐second☐third☐fourth☐fifth☐ where the box represents the null character. Next, memory for an array of six (argc) pointers is allocated and these six pointers are assigned the first character of each of the strings into which the command line was parsed. Finally, memory is allocated for the stack and using 8-byte alignment, the two arguments are placed into the stack. This is shown in Figure 1.

The result of calling ./a.out first second third fourth fifth on the command line

Figure 1. The result of calling ./a.out first second third fourth fifth on the command line.

Further Fun

In Unix, the shell does further processing of the command. For example, if it finds wild cards that indicate files in the current directory, it will attempt to expand those wild cards. These include ? for one unknown character and * for any number of characters.

For example, the following examines the file in the current directory:

% ls
a.out  argument.0.c  argument.1.c  content.html  images  index.html  primary.0.html  src
% ./a.out *
argc:     9
argv[0]:  ./a.out
Arguments:
  1. a.out
  2. argument.0.c
  3. argument.1.c
  4. content.html
  5. images
  6. index.html
  7. primary.0.html
  8. src
% ./a.out *.html
argc:     4
argv[0]:  ./a.out
Arguments:
  1. content.html
  2. index.html
  3. primary.0.html
% ./a.out argument.?.c
argc:     3
argv[0]:  ./a.out
Arguments:
  1. argument.0.c
  2. argument.1.c

Similarly, you can tell the command line to treat spaces as part of one string or ignore wildcards by either using the backslash before the space or wildcard or surrounding the string by single or double quotes.

% ./a.out hi\ there\?
argc:     2
argv[0]:  ./a.out
Arguments:
  1. hi there?
% ./a.out "hi there?"
argc:     2
argv[0]:  ./a.out
Arguments:
  1. hi there?
% ./a.out 'hi there?'
argc:     2
argv[0]:  ./a.out
Arguments:
  1. hi there?
%

Finally, if you surround text with backticks, it will execute that first and then replace that with the output of the command:

% ./a.out `ls *.c`
argc:     4
argv[0]:  ./a.out
Arguments:
  2. argument.0.c
  3. argument.1.c
%

Guess what happens if you enter ./a.out ls -al.

Disclaimer: This above post is directly taken from https://ece.uwaterloo.ca/~dwharder/icsrts/C/05/. It was originally written by Douglas Wilhelm Harder of U Waterloo. If it totally belongs to another person, then why did I put it here? Because I read his post and I learned new things and I liked it so much that I put it here so that other people also get the benefit from it. Again, all hard-work and applauds go to Mr Harder, not me. Thanks.

0
4

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

2
  • 7
    the first entry in argv[0] is the program name, not an argument Dec 21, 2014 at 8:10
  • @user3629249 Program name with program path. ;) Jul 20, 2018 at 8:10
2

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.

4
  • 5
    Might want to put void in... int main() ==> int main(void)... for compatibility and readability. I don't know if all older versions of C allow void functions to have an empty parameter list in declaration.
    – dylnmc
    Sep 25, 2014 at 16:24
  • 1
    @dylnmc this doesn't give any readability gain, and is exactly equivalent in all C++ versions. Only in C this does have a difference, but only in declarations, not in definition.
    – Ruslan
    May 5, 2015 at 9:35
  • 1
    @Ruslan Sorry, I posted this when I was just learning C, and I might have read that in very early versions of C the void is required. Don't quote me on that, and I now know it is a slightly foolish comment. It can't hurt, though.
    – dylnmc
    May 7, 2015 at 1:41
  • what if argc<3 returns an error? what could possibly went wrong?
    – AVI
    Dec 4, 2017 at 15:50
2

Just to add because someone says there is a third parameter (*envp[]), it's true, there is, but is not POSIX safe, if you want your program to use environment variables you should use extern char **environ ;D

Not the answer you're looking for? Browse other questions tagged or ask your own question.