C – Command Line Arguments


Command Line Arguments in C



”;


In any C program, there may be multiple functions, but the main() function remains the entry point from where the execution starts. While the other functions may have one or more arguments and a return type, the main() function is generally written with no arguments. The main() function also has a return value of “0”.


int main() { 
   ... 
   ...

   return 0;
}

Inside the main() function, there may be scanf() statements to let the user input certain values, which are then utilized.


#include <stdio.h>

int main() {

   int a;
   scanf("%d", &a);
   printf("%d", a);

   return 0;
}

What are Command Line Arguments?

Instead of invoking the input statement from inside the program, it is possible to pass data from the command line to the main() function when the program is executed. These values are called command line arguments.

Command line arguments are important for your program, especially when you want to control your program from outside, instead of hard coding those values inside the code.

Let us suppose you want to write a C program “hello.c” that prints a “hello” message for a user. Instead of reading the name from inside the program with scanf(), we wish to pass the name from the command line as follows −


C:usersuser>hello Prakash

The string will be used as an argument to the main() function and then the “Hello Prakash” message should be displayed.

argc and argv

To facilitate the main() function to accept arguments from the command line, you should define two arguments in the main() function – argc and argv[].

argc refers to the number of arguments passed and argv[] is a pointer array that points to each argument passed to the program.


int main(int argc, char *argv[]) { 
	... 
	...

	return 0;
}

The argc argument should always be non-negative. The argv argument is an array of character pointers to all the arguments, argv[0] being the name of the program. After that till “argv [argc – 1]“, every element is a command-line argument.

Open any text editor and save the following code as “hello.c” −


#include <stdio.h>

int main (int argc, char * argv[]){
   printf("Hello %s", argv[1]);
   return 0;
}

The program is expected to fetch the name from argv[1] and use it in the printf() statement.

Instead of running the program from the Run menu of any IDE (such as VS code or CodeBlocks), compile it from the command line −


C:Usersuser>gcc -c hello.c -o hello.o

Build the executable −


C:Usersuser>gcc -o hello.exe hello.o

Pass the name as a command line argument −


C:Usersuser>hello Prakash
Hello Prakash

If working on Linux, the compilation by default generates the object file as “a.out“. We need to make it executable before running it by prefixing “./” to it.


$ chmod a+x a.o
$ ./a.o Prakash

Example

Given below is a simple example that checks if there is any argument supplied from the command line and takes action accordingly −


#include <stdio.h>

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

   if(argc == 2) {
      printf("The argument supplied is %sn", argv[1]);
   }
   else if(argc > 2) {
      printf("Too many arguments supplied.n");
   }
   else {
      printf("One argument expected.n");
   }
}

Output

When the above code is compiled and executed with a single argument, it produces the following output −


$./a.out testing
The argument supplied is testing.

When the above code is compiled and executed with two arguments, it produces the following output −


$./a.out testing1 testing2
Too many arguments supplied.

When the above code is compiled and executed without passing any argument, it produces the following output −


$./a.out
One argument expected

It should be noted that argv[0] holds the name of the program itself and argv[1] is a pointer to the first command line argument supplied, and *argv[n] is the last argument. If no arguments are supplied, then argc will be set at “1” and if you pass one argument, then argc is set at “2“.

Passing Numeric Arguments from the Command Line

Let us write a C program that reads two command line arguments, and performs the addition of argv[1] and argv[2].

Example

Start by saving the code below −


#include <stdio.h>

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

   int c = argv[1] + argv[2];
   printf("addition: %d", c);
   
   return 0;
}

Output

When we try to compile, you get the error message −


error: invalid operands to binary + (have ''char *'' and ''char *'')
 int c = argv[1]+argv[2];
         ~~~~~~~~~~~~~~

This is because the “+” operator cannot have non-numeric operands.

The atoi() Function

To solve this issue, we need to use the library function atoi() that converts the string representation of a number to an integer.

Example

The following example shows how you can use the atoi() function in a C program −


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

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

   int c = atoi(argv[1]) + atoi(argv[2]);
   printf("addition: %d", c);
   
   return 0;
}

Output

Compile and build an executive from “add.c” and run from the command line, passing numeric arguments −


C:Usersuser>add 10 20
addition: 30

Example

You pass all the command line arguments separated by a space, but if the argument itself has a space, then you can pass such arguments by putting them inside double quotes (” “) or single quotes (” ”).

In this example, we will pass a command line argument enclosed inside double quotes −


#include <stdio.h>

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

   printf("Program name %sn", argv[0]);

   if(argc == 2) {
      printf("The argument supplied is %sn", argv[1]);
   }
   else if(argc > 2) {
      printf("Too many arguments supplied.n");
   }
   else {
      printf("One argument expected.n");
   }
}

Output

When the above code is compiled and executed with a single argument separated by space but inside double quotes, it produces the following output −


$./a.out "testing1 testing2"

Program name ./a.out
The argument supplied is testing1 testing2

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *