Logo  

CS256 - Fundamentals of Structured Programming

Displaying exercises/e5/solution/h4.c

#include <stdio.h>

/**
 * Read the chapter on command line arguments:
 * https://www.tutorialspoint.com/cprogramming/c_command_line_arguments.htm
 * or:
 * http://cs.indstate.edu/~sbaker/cs256/book/C31-Command_Line_Arguments.pdf
 * 
 * Print up to the first three command line parameters that follow the command
 * itself. Print them each on their own line, prefixed with their index
 * number.  Print the parameters only if they are actually there and each on
 * their own line.
 * Example input/output:
 * ./h4 a b
 * 1: a
 * 2: b
 *
 * ./h4 a b c d e f
 * 1: a
 * 2: b
 * 3: c
 */

int main(int argc, char *argv[])
{
  if (argc > 1) printf("1: %s\n", argv[1]);
  if (argc > 2) printf("2: %s\n", argv[2]);
  if (argc > 3) printf("3: %s\n", argv[3]);
  return 0;
}