Example of Arguments to main

The following program backward prints the arguments entered on a command line such that the last argument is printed first:

#include <stdio.h>
int main(int argc, char *argv[])
{
  while (--argc > 0)
    printf("%s ", argv[argc]);
}

Invoking this program from a command line with the following:

   backward string1 string2

gives the following output:

   string2 string1

The arguments argc and argv would contain the following values:

Object Value
argc 3
argv[0] pointer to string "backward"
argv[1] pointer to string "string1"
argv[2] pointer to string "string2"
argv[3] NULL
Note:
Be careful when entering mixed case characters on a command line because some environments are not case-sensitive. Also, the exact format of the string pointed to by argv[0] is system-dependent.

Related References

IBM Copyright 2003