Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Command-Line Arguments

C Command-Line Arguments

When executing a program, values can be passed to a C program from the command line. These values are calledCommand-Line Arguments, and they are important to the program, especially when you want to control the program from the outside rather than hard-coding these values in the code.

In C, command-line arguments are a way to get input from the command line and can be used to pass information to a program when it runs. Command-line arguments are passed to the program through the parameters of the main function. The prototype of the main function can take one of the following two forms:

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

Or:

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

The following is a simple example that checks whether arguments are provided on the command line and performs the corresponding action based on them:

Example

#include

int

main

(

int

argc

,

char

argv

[

]

)

 

{

   

if

(

argc

==

2

)

   

{

     

printf

(

"The argument supplied is %s\n"

,

argv

[

1

]

)

;

   

}

   

else

if

(

argc

2

)

   

{

     

printf

(

"Too many arguments supplied.\n"

)

;

   

}

   

else

   

{

     

printf

(

"One argument expected.\n"

)

;

   

}

}

Using one argument, compile and execute the code above, and it produces the following result:



$./a.out testing

The argument supplied is testing

Using two arguments, compile and execute the code above, and it produces the following result:



$./a.out testing1 testing2

Too many arguments supplied.

Without passing any arguments, compile and execute the code above, and it produces the following result:



$./a.out

One argument expected

It should be noted thatargv[0] stores the program name,argv[1] is a pointer to the first command-line argument, and *argv[n] is the last argument. If no arguments are provided, argc will be 1; otherwise, if one argument is passed,argc will be set to 2.

Multiple command-line arguments are separated by spaces, but if an argument itself contains spaces, then when passing it you should place it inside double quotes "" or single quotes ''. Let us rewrite the example above: with a space, you can put it inside double or single quotes """" by this view. Let us rewrite the example above to pass a command-line argument placed inside double quotes to the program:

Example

#include

int

main

(

int

argc

,

char

argv

[

]

)

 

{

   

printf

(

"Program name %s\n"

,

argv

[

0

]

)

;

 

   

if

(

argc

==

2

)

   

{

     

printf

(

"The argument supplied is %s\n"

,

argv

[

1

]

)

;

   

}

   

else

if

(

argc

2

)

   

{

     

printf

(

"Too many arguments supplied.\n"

)

;

   

}

   

else

   

{

     

printf

(

"One argument expected.\n"

)

;

   

}

}

Using a simple argument separated by spaces, with the argument enclosed in double quotes, compile and execute the code above, and it produces the following result:



$./a.out "testing1 testing2"



Progranm name ./a.out

The argument supplied is testing1 testing2

Use Cases

Command-line arguments are useful in many situations, for example:

Notes