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

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

In C++, int argc and char *argv[ ] are the command-line arguments passed to the main function when the program starts executing to check and store the number of arguments passed to the program.

When working with C++ programs that take command-line arguments, we will often see the main function defined as int main (int argc, char *argv[ ]). This syntax allows a program to accept user input via the command line when it is executed. In this article, we will discuss the int argc and char *argv[] with their practical application and advanced considerations.

Table of Contents:

Understanding argc and char*argv[ ] in C++

1. int argc

  • argc (Argument Count) is an integer that represents the number of arguments passed to the program from the command line, including the program’s name itself.
  • If a program is run without any arguments, argc is 1 since the program name is always counted.
  • If additional arguments are provided, argc increases accordingly.

2. char *argv[]

  • argv (Argument Vector) is an array of character pointers that stores the arguments passed to the program.
  • argv[0] is always the name of the program.
  • argv[1] to argv[argc – 1] stores the actual arguments passed by the user.

Example:

Cpp

Output:

In this code, int argc, char*argv[ ] is used in the main() function to check the command-line arguments. argc gives the number of arguments, and argv[ ] prints all the arguments from the file.

Practical Applications of int argc, char *argv[ ] arguments

Below are some practical applications for these int argc, char *argv[ ] command-line arguments:

  • Programs can process different types of command-line arguments in a dynamic environment.
  • Programs such as “grep” and “ls” accept the filenames as arguments.
  • Arguments enable the scripts to execute with pre-planned parameters, enabling automation purposes and batch processing.
  • Command-line arguments can be used to select graphics settings or to activate debug models.
  • Applications can be used to accept arguments for IP addresses, ports, or authenticating credentials for establishing secure network connections.

Advanced Considerations

For larger applications, which make use of libraries like boost::program_options provides a more structured way to deal with the command-line arguments. It also allows for named options, default values, and automatic help messages.

Example:

Cpp

Output:

In this code, boost::program_options is used to manage the command-line arguments. Under this boost library, argc and argv are used to check and store the passed arguments in the program. This is used only for larger applications and in the advanced C++ versions.

Const correctness in int argc, char*argv[ ] in C++

In C++, const correctness is an important concept, as it improves the readability of the code for the compiler. const is used to prevent modifications in the command-line arguments. It can be applied in many ways to stop the strings and pointers from getting modified.

1. const char* argv[]

When const is applied before the char, it prevents the strings from getting modified, but the pointers can be modified.

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

Example:

Cpp

Output:

In this code, const is used before char, that’s why only the pointer is modified, and the string cannot be modified. The modified pointer is printed as the output.

2. char* const argv[]

When const is applied after the char, it prevents the pointers from being modified, but the strings can be modified easily.

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

Example:

Cpp

Output:

In this code, const is used after char, that’s why only strings are modified and pointers cannot be modified. The modified string is printed as an output.

Using char** argv in C++11

In C++11 or advanced versions, char** argv is majorly used with pointers for writing complex codes. It is equivalent to char* argv[] as both provide clarity and readability in the codes for the compiler. So, char** argv is another form of char* argv[] because they both have the same functionality.

Example:

Cpp

Output:

In this code, char** argv is used instead of char*argv[], but the output is the same as they both have the same functionality.

Conclusion

Knowing int argc, char *argv[] in C++ is very important for writing flexible and interactive programs. Using these command-line arguments, C++ developers can create software that can easily handle different user input types and application usage. These command-line arguments typically play an important role in automation, configuration, and program interaction, making them a crucial feature in C++ for application

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner