Retrieving command line arguments

This program demonstrates how to access and retrieve the arguments passed via the command line. It simply echoes each command line argument back to standard out.

Programming Issues

int argc contains the number of command line arguments. This is required to be non-negative, but most environments set the first argument to the name of the program, or its path, so in practice argc is usually at least 1.

char *argv[] is an array of pointers to type char, and points to the actual command line arguments themselves. Thus, argv[0] points to the first command line argument, and argv[argc-1] points to the last one.

Note that all command line arguments are supplied to your program as strings. If you need numerical arguments, then you will have to convert them using functions such as strtol and strtod (see the Number Guesser application for an example of this). See the Towers of Hanoi example for a demonstration of how to receive and parse command line switches, or options.

Usage

Experiment with supplying different command line arguments, e.g.:

cmdline
cmdline 1 2 3
cmdline hello world
cmdline "hello world"
cmdline what does "this do?"
cmdline 'what does "this do?"'
cmdline 'ls'
cmdline `ls`

Source and Downloads