liveBook · Manning

Command-line argument

As explained in Modern C, Third Edition

A command-line argument is a string value supplied to a program when it is invoked from a command-line environment.

A command-line argument is a string supplied when a program is invoked. In C, a form of main receives these strings through argv: int main(int argc, char* argv[argc+1]);. The program decides how to interpret their contents. section 7.2

Converting an argument

double const a = strtod(argv[i], nullptr); converts the string at argv[i] to a double value. The argument arrives as a string even when the program intends to use it as a number. section 7.2

The argv array has two special entries: argv[0] points to the name of the program invocation, usually the executable name, while argv[argc] is a null pointer. The null pointer marks the end of the array; argc can be used to process its entries. section 7.2

Arguments to a compiler

The command c17 -Wall -o getting-started getting-started.c -lm invokes a compiler with several arguments. Compiler names and command-line arguments depend on the platform. section 1.2

TAKEAWAY: Command-line arguments arrive as strings; interpreting them is the program’s responsibility. They are distinct from arguments in a C function call, whose values are passed to the called function. section 7.2 section 2.4.2

How Modern C, Third Edition covers it

How deep it goes
Explains the main interface, special argv entries, and string conversion, without detailing command-line parsing.
Examples
Illustrates a compiler command and conversion of an argv entry to double with strtod.
What you can do after
Understand how to access command-line strings and interpret them as double values with strtod.
How it connects
Connects command-line arguments to main, strings, pointers, strtod, and function calls.

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, Third Edition. The section links point to the parts of the book it draws on.