Example of command-line arguments in C

This C program illustrates the use of arguments on the command-line.

#include <stdio.h>

int main (int argc, char ** argv)
{
    int i;

    printf ("There are %d command line arguments.\n", argc);

    for (i = 0; i < argc; i++) {
        printf ("Argument %d is '%s'.\n", i, argv[i]);
    }
    return 0;
}

Download it here.

When invoked in the form

./cl hello world
the output of the example looks like this:
There are 3 command line arguments.
Argument 0 is './cl'.
Argument 1 is 'hello'.
Argument 2 is 'world'.

Ask and answer questions on C in the new C forum

Copyright © Ben Bullock 2009-2012. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (ben.bullock@lemoda.net) / Privacy / Disclaimer