C Command Line Arguments

In this tutorial you will learn about the C Command Line Arguments and its application with practical example.

In C Programming Language we have built-in support for “Command Line arguments”. Command-line arguments allow us to pass the parameters to the C Program’s main() function when they are executed. We can pass the parameters from the command line itself.

Table Of Contents

Below is the prototype for main() when it is supposed to accept command-line arguments –

syntax:

There are two parameters passed to the main() function.

  • int argc
  • char *argv[]

The first parameter, argc is an integer that is used to define the number of parameters passed to main(), and the second parameter is argv, is an array of pointers to character strings which is consist of the list of these parameters. Each argument on the command line is separated by
spaces.

The program name will be stored in the first item in argv, followed by the list of the parameters. If the program name is followed by n parameters there will be n + 1 element in argv, ranging from argv[0] to argv[n]. argc will be automatically set equal to n + 1.

Example :

Assuming that the executable is called hello.

In this tutorial we have learn about the C Command Line Arguments and its application with practical example. I hope you will like this tutorial.