c++ – How argc and argv work

Question:

Please tell us about how the argc and argv arguments work in C / C ++ .

Answer:

  • argv is an array of pointers to null-terminated strings containing the command line options your program was invoked with.

  • If argc greater than zero, then argv[0] contains a pointer to the name of your program. How this name is presented is implementation dependent. If no program name is supplied, argv[0] will point to an empty string (that is, it cannot be a null pointer).

  • If argc greater than one, then argv[1]argv[argc - 1] contain pointers to command line parameters .

  • The size of this array is argc + 1 (not argc , as is often mistakenly believed). This ensures that argv[argc] contains a null pointer. Thus, to find the end of the argv array, you can either use the argc value, or simply browse the argv array until you encounter the first null pointer.

  • In the C language, it is allowed to modify both the elements of the argv array and the strings themselves indicated by the elements of the argv array (of course, within the original string length). C ++ does not explicitly grant this permission.

Scroll to Top