Summary
We have explored the simplest way to provide input to our programs via the command line. We first specified how the main()
function can receive arguments that contain the count and values of arguments given to the program. We saw how argc
and argv
are related, and how to access each argv
string. A simple program to print out arguments given to it was provided for further experimentation. We noted how all arguments are passed into main()
as strings. Once we access those arguments, we can perform further processing on them to alter the behavior of our program. Finally, a very simple command-line processor was provided to demonstrate the use of the getopt()
C Standard Library function.
In the next chapter, we will explore a more comprehensive way to receive input from the user while a program is running. Just as printf()
writes formatted data from program variables to the console (screen), the&...