ChatGPT解决这个技术问题 Extra ChatGPT

What does int argc, char *argv[] mean?

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?

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.
A hint for hackers: try declaring int main(int argc, char* argv[], char* envp[]) and printing out the last argument. ;)
@ulidtko it is not good that you are teaching newbies to introduce vulnerability in their programs ;)
@Gab how's simple printing of environment variables lead to vulnerability? Just don't pass the tainted strings verbatim to system() calls, DB queries, etc. As usual with user input.
@ulidtko Interesting.. Can you expound why you don't have to pass tainted strings, db queries, etc. while using char **envp argument?

u
user229044

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

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.
But then again, if argc is 0 and argv NULL, then surely argv[argc] = *(NULL + 0) = *NULL = NULL = 0, right?
@EmilVikström No, that's a serious error that probably results in a segfault. *NULL is definitely not equal to NULL.
@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).
@BiMo You have to pass it 3 CLI arguments in order to produce that output. See the command ./test a1 b2 c3.
P
Peter Mortensen

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
}

T
Toby Speight

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:

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. *argv[] and **argv are exactly equivalent, so you can write int main(int argc, char *argv[]) as int main(int argc, char **argv).


If we're being technical, basic.start.main/2 explicitly allows implementation-defined additional versions of main(), provided that the implementation provides the two predefined versions. So, they're not exactly non-conforming. The most common one is envp, which is so well-known in both C and C++ that it's literally the very first entry in section J.5 (Common extensions) of the C standard.
Thanks for the nice pedantry @Justin. Answer updated to be more correct.
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.
a
adrian
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.


A
Azeem

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.


Argv[] always has argv[arg] as a null pointer. and Argv[0] is always the (full path)/executableName as a nul terminated string
@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.
A
Azeem

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.


N
Nick Gerakines

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


the first entry in argv[0] is the program name, not an argument
@user3629249 Program name with program path. ;)
A
Anoop Rana

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:

https://i.stack.imgur.com/4TpCz.png

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).


C
Community

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.


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 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 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.
what if argc<3 returns an error? what could possibly went wrong?