liveBook · Manning

Command-line argument

As explained in Modern C

A command-line argument is a text value supplied to a program when it is invoked.

int main can receive command-line arguments through argc and argv. Each argument is supplied as text when the program starts, then interpreted by the program according to its needs. section 7.2

int main(void);
int main(int argc, char* argv[argc+1]);


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":3}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":3}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":8}],[{\"line\":1,\"ch\":4},{\"line\":1,\"ch\":8}],[{\"line\":0,\"ch\":9},{\"line\":0,\"ch\":13}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":3}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":3}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":8}],[{\"line\":1,\"ch\":4},{\"line\":1,\"ch\":8}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":3}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":3}],[{\"line\":1,\"ch\":19},{\"line\":1,\"ch\":23}]]"}
!@%STYLE%@!

The two common main prototypes contrast a program with no parameters and one receiving argc plus argv. section 7.2

TAKEAWAY

  • All command-line arguments are transferred as strings; the program must interpret those strings itself. section 7.2
  • argv[0] usually contains the name used to invoke the program, commonly its executable name. section 7.2
  • argv[argc] is 0, providing a terminating value beyond the actual command-line arguments. section 7.2

Converting an argument

A program that expects numeric input can treat an argv element as a string and use strtod to decode it as a double. The conversion is a program decision, not an automatic property of the command line. section 7.2

Do not confuse command-line arguments with the arguments in a C function call. Function-call arguments pass values to the called function; command-line arguments arrive through main as strings. section 2.4.2 section 7.2

How Modern C covers it

How deep it goes
Explains argc, argv, special argv entries, and string-to-double decoding, while deferring broader pointer mechanisms.
Examples
Uses main prototypes and a numeric-input example that decodes argv strings as double values with strtod.
What it assumes
Assumes familiarity with C functions and pointers while teaching that command-line values enter main as strings.
What you can do after
Apply it via main and strtod to receive command-line text and interpret selected arguments as double data.
How it connects
Connects command-line arguments to main, strings, pointers, argc, argv, and strtod.

Also explained in

Compare how 14 books explain command-line argument →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.