Command line arguments in C programming with examples

Command-line-arguments-in-c-programming

Introduction:

The Command line arguments are one of the ways to pass input to programs. In this article, We are going to discuss different ways to provide input to the program and We will learn what are command-line arguments and how to use them to provide input to the program. We will also look into examples of command line arguments to make understanding better.

Examples use of command line arguments:

We will pass two arguments at the command line along with the executable file.

Input:

output:

Note: Here ./a.outis our executable file. I am presently using linux so ./a.outis the executable file extension in linux. If you are in Windows OS, you are executable file will have .exe file extension.

Here is an another example

Input:

Output:

Command Line Arguments in C :

Before going to discuss about the command line argument. Let’s see how program gets the input from the user.

Generally, we use the scanf function to take the data or input from the user. Here is an example program to get data from user using the scanf function.

The scanf function to read the data from user:

In the above program, we asked user to provide two values. Once the user provides input, We are going to use scanf function to store the user input to variables a and b. The first value will be stored in variable 'a'and the second value will be stored in the variable 'b'.

Then we printed the stored values of variable 'a' and variable 'b'.

Output:

But scanf function is the not only way to provide the input to the program. We can also use the Command line arguments to pass the input to program.

Command line arguments to provide input to program

When we compile our code using GCC or any other compiler, We get the executable file that is a.out (In the Linux and Mac). And we are going to run this executable file using by specifying its path at the command line like below.

So here we are manually executing the program through the command line. The C language allows us to pass extra parameters along with the executable file while running/executing the program(executable file). These extra parameters are called command line arguments.

For example,

In above example, ./a.out is our executable file and parameters 10 and 20 are command line arguments. The operating system will pass these parameters 10 and 20 to program while executing. So They are available for us to access from the code.

These variable 10 and 20 are stored in aspecial array called argument vector or "argv" and we also have one more variable called argument count or "argc". And argc contains the number of elements passed through the command line.

Note: The argc value contains the total number of parameters passed through the command line, argc value also includes the executable file name as well, In the above example, argc value will be 3 i.e ./a.out 10 20

Example Program: Add two integers given at command line:

Here is an example to add two integers which are passed at the command line.

In the above program, We are taking two integers from the user. The user need to pass those two integers at the command line while running the program.

Above program need three arguments, They are

So User needs to provide the executable file along with two integers to calculate the sum. If the user input is not matched with the expected input, We are going to display the error message with usage policy by specifying how to use the executable file. Like

If the user provides the executable with the two integer numbers, Then our argc check will pass and program continues.

Our command-line arguments are stored in the argv array. The First integer is stored at argv[1] and second integer stored at argv[2]

The file name in our case ./a.out is stored in the argv[0]

By default, all command-line arguments are of String format. So two integers passed at the command line also in the string format. So we need to convert them from string to integer. To do that we are going to use the atoi() function. which will perform the ASCII to Integer conversion.

The atoi()function is available as part of the stdlib.h headerfile. So we need to include the stdlib.h headerfile as well.

📢 The atoi() function is used to perform the ASCII to integer conversion. As all command-line arguments are strings we need to convert them to integer before performing the addition.

Once we convert the values to integer, We will perform the addition and display the result on to the console.

Output:

Related Article:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

5 Responses

  1. […] Command line arguments in C programming with examples – SillyCodes […]

  2. […] Command-line arguments in C programming with examples – SillyCodes […]

  3. […] Command-line arguments in C programming with examples – SillyCodes […]

  4. […] Command line arguments in C programming with examples – SillyCodes […]

  5. […] Command-line arguments in C programming with examples – SillyCodes […]

Leave a Reply