乐闻世界logo
搜索文章和话题

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

1个答案

1

In C or C++ programs, when you run a program from the command line, int argc and char *argv[] are two variables used to receive command-line arguments, serving as parameters to the main function. These parameters provide a way for users to pass input information to the program.

  • int argc: This variable represents the number of command-line arguments passed to the program. argc is an abbreviation for 'argument count'. Its value is at least 1, as the first argument is the program name by default.
  • char *argv[]: This is a string array used to store the actual argument values. argv is an abbreviation for 'argument vector'. argv[0] is the program name, argv[1] is the first argument passed to the program, and so on, up to argv[argc-1].

Example:

Suppose you have a program named example, and you run it from the command line as:

shell
./example hello world

Here, argc will be 3 because there are three arguments: the program name ./example, hello, and world.

argv[0] will be the string './example', argv[1] will be the string 'hello', and argv[2] will be the string 'world'.

This mechanism is very useful, for example, when you need to pass file names, configuration options, or other data to the program before running it.

2024年8月9日 17:36 回复

你的答案