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;


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'.

Copyright © Ben Bullock 2009-2024. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer