SoFunction
Updated on 2025-04-06

Use of parameters of int main(int argc, char** argv) in C++

int main(int argc, char** argv)is the entry point of C and C++ programs, whereargcandargvis used to receive parameters passed to the program from the command line. Below I will explain the meaning of these two parameters in detail and give an example to help understand.

Parameter explanation

  • int argc

    • argcis the abbreviation of "argument count", which represents the number of command line parameters passed to the program.

    • It is at least 1, because there is always at least one parameter on the command line: the name of the program.

  • char** argvorchar *argv[]

    • argvis the abbreviation of "argument vector", which is a pointer to an array of character pointers.

    • Each elementargv[i]They are all pointers to C strings, which contain the numberiCommand line parameters.

    • As usual,argv[0]It is the name of the program.

    • argvThe array ends with a NULL pointer, but this NULL pointer is not usually included inargcInside the count.

Give an example

Suppose you have the following C++ program:

#include <iostream>  
  
int main(int argc, char** argv) {  
    std::cout << "Program name: " << argv[0] << std::endl;  
    for (int i = 1; i < argc; ++i) {  
        std::cout << "Argument " << i << ": " << argv[i] << std::endl;  
    }  
    return 0;  
}

If you compile this program tomy_programAnd run it from the command line as follows:

./my_program hello world 123
Program name: ./my_program  
Argument 1: hello  
Argument 2: world  
Argument 3: 123

In this example:

  • argcThe value of  is 4 because there are four parameters: program name (./my_program) and three user-provided parameters (helloworld123)。

  • argv[0]Point to string"./my_program"

  • argv[1]Point to string"hello"

  • argv[2]Point to string"world"

  • argv[3]Point to string"123"

  • Note that there is noargv[4], because the array index starts at 0 and in this example there are only four elements. Visitargv[4]Create undefined behavior because it exceeds the boundaries of the array. In fact, according to the C and C++ standards,argvArrays are not guaranteed to end with a NULL pointer, although this may be the case on some systems. You should always rely onargcto determine the number of parameters.

Examples based on OCCT:

This is a simple C++ program that is used to read STEP files and convert their contents into another format.

#include <STEPControl_Reader.hxx>
​
int main(int argc, char** argv)
{
  // Read from file.
  TopoDS_Shape shape;
  //
  if ( argc > 1 )
  {
    STEPControl_Reader readerBase;
    (argv[1]);
    std::cout << () << "Base roots transferred" << std::endl;
  }
  else
  {
    std::cout << "Please, pass filename (STEP) as an argument." << std::endl;
    return 1;
  }
​
  return 0;
}
​

The difference between char** argv and char* argv[]

  • The pointer is an address.
  • char *a: The value of a contains character data. Unreference to a (*a) gets a character, and does not dereference to a character string. printf("%c",*a) outputs a character or printf("%s", a) outputs a string;
  • char **a: The value of a contains an address, which contains character-type data. Unreference to a (*a), get the address, and then unreference to the address (**a), get a character, printf("%c",**a) outputs a character printf("%s",*a) outputs a string;
  • char *a[]: a is an array, the elements of the array are addresses (the address is actually a pointer), and the values ​​contained in the address are character data. printf("%c",*a[i]) outputs a character, printf("%s", a[i])) outputs a string;
  • char *a or char **a can change the value of a, char a*[] cannot change the value of a.

This is the article about the use of parameters of int main(int argc, char** argv) in C++. For more related contents of C++ int main(int argc, char** argv) please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!