Solution for Exercise 15-4

ex1504

#include <stdio.h>

int main(int argc, char *argv[])
{
    int x;

    for(x=0;x<argc;x++)
        printf("Arg#%d = %s\n",x+1,argv[x]);
    return(0);
}

Output

./a.out Shadrach Meshach Abednego
Arg#1 = ./a.out
Arg#2 = Shadrach
Arg#3 = Meshach
Arg#4 = Abednego

Notes

* The program name is a.out, shown above.

* In Code::Blocks, you need to use the Project→Set Programs' Arguments command to supply the program with command line arguments.

* The value of argc is used in the for statement at Line 7.

* Again, the x+1 convention is used in Line 8 so that humans reading the output won't be confused by element zero in the string array.