C Tutorial – Command Line Parameter Parsing

In this tutorial we take another look at command line parameter parsing with C programs. In a previous command line parameter tutorial we already looked at some simple command line argument parsing example. Please read that tutorial before advancing below.

More Complex Command Line Parameter Parsing Example

Command-line parameters can be used for many things. Let’s take a look at another example that will print different things depending on the flags given. If the –f flag is used then the given argument is printed once.
If the –d flag is used the argument is printed twice.

Let’s look at the example:


#include <stdio.h>
#include <stdlib.h>

void usage(void)
{
	printf("Usage:\n");
	printf(" -f<name>\n");
	printf(" -d<name>\n");
	exit (8);
}

int main(int argc, char *argv[])
{
	printf("Program name: %s\n", argv[0]);

	while ((argc > 1) && (argv[1][0] == '-'))
	{
		switch (argv[1][1])
		{
			case 'f':
				printf("%s\n",&argv[1][2]);
				break;

			case 'd':
				printf("%s\n",&argv[1][2]);
				printf("%s\n",&argv[1][2]);
				break;

			default:
				printf("Wrong Argument: %s\n", argv[1]);
				usage();
		}

		++argv;
		--argc;
	}
	return (0);
}

After compiling you can run the program with the following parameters:

  • # program
  • # program –fhello
  • # program –dbye
  • # program –fhello –dbye
  • # program –w

Note: that there is no space between the flag and the flag argument.

Closer Look at the Program

First the program name is printed (it will always be printed.)

The “while loop” looks for the dash sign. In case of the –f the flag argument is printed once. If the flag is –d then the flag argument is printed twice.

The argv[][] array will be used as follows:

For example: –f<name>

  • argv[1][0] contains the dash sign
  • argv[1][1] contains the “f”
  • argv[1][2] contains the flag argument name

If a wrong flag sign is given then the usage is printed onto the screen.

Experiment Yourself

If you want to experiment further with command line parsing then you should expand the program above with the ability to add a space between the flag and the flag argument. Another thing you could add is parsing the program name to the usage function and prints it from this function.

That’s all for this tutorial.

This entry was posted in C Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. Tweet This! Tweet This! or use to share this post with others.

There are currently 2 responses to “C Tutorial – Command Line Parameter Parsing”

Why not let us know what you think by adding your own comment!

  1. Steve on December 6th, 2009:

    This is really neat. I just finished a program myself that opens a file and prints its contents to a second file using the command line. My next project is to develop something similar to the UNIX fold command and I was wondering how you would go about adding the ability to have a space between the flag and flag argument.

  2. Utkarsh on December 21st, 2012:

    Precise. For more flexibility, a match function call for each case can be added, which can again do some parsing on the value.

Leave a Reply: