Command-Line Arguments

Lesson 17 of 20

Your program just got arguments: argc counts the words, argv holds them — see the shell hand them over.

The big picture

main gets extra info

Anything typed after the program name arrives in main as argc and argv — a count and a list of words.

main gets extra info

main(int argc, char *argv[]) receives what the user typed after the program name.

argc counts

argc is the number of words on the command line, including the program name itself.

argv holds the words

argv[0] is the program name; argv[1], argv[2]... are the arguments the user typed.

Everything is text

Arguments arrive as strings — use atoi (from stdlib.h) to turn "25" into the number 25.