C Programming

The C Command Line Arguments Processing

C programming language provides us with a way to pass command line arguments in Linux. This article will enlighten you more on this concept by discussing how the command line arguments are processed in C. Moreover, we will look at a suitable example that will possibly remove all your ambiguities regarding the said concept.

Usage of Command-Line Arguments in C

As we already stated, the command-line arguments in C are used to provide values to your program during its execution. These arguments are specified with the “main()” function of your C code in the following manner:

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

Now, we will discuss these two newly introduced parameters of the “main()” function one by one. The first parameter, i.e., argc is there to keep track of the number of the provided runtime arguments. It stands for “argument count”. However, an important thing to be noted over here is that the first argument is always the “program name” by default. Thus, the value of this counter is always “1” more than the number of command-line arguments provided at the runtime.

Coming towards the second parameter, i.e., char* argv[]. This character array holds all those arguments you provide at the runtime, along with the name of your program. Again, the “0th” index of this character array, i.e., argv[0] will correspond to the name of your program, and the subsequent indexes of this array will store all those command-line arguments that you will provide at the runtime.

Now, we will look at an example of using the command-line arguments in the C programming language in Linux.

Example of Using the Command-Line Arguments in C

In this example, we will simply see how we can pass command-line arguments at the runtime through the terminal in C. We have a C file named “CLA.c”. In this file, we defined the C code as our “main()” function after importing the required library. Our “main()” function this time is different from a regular “main()” function since it is parameterized. It consists of the “argc” and “argv[]” parameters whose details we have already explained above.

In the body of our “main()” function, we have first printed the value of the “argc” parameter, which will give us the total provided argument count at runtime. Again, this argument count will be “1” more than the actual number of arguments provided. For example, if you will provide two arguments at runtime, then this argument count will be three. After that, we have a “for loop” that iterates through the indexes of the “argv[]” array. In this loop, we intend to print the values of the arguments provided through the command line at the runtime.

Once we had saved our C code, we created its object file with the command shown below:

$ gcc CLA.c –o CLA

After compiling our code successfully, we can efficiently execute it. Now, it is time to provide the command-line arguments while running our code like this:

$ ./CLA String1 String2 String3 …

After the name of your object file, you can provide as many string arguments as you want. We did the same, as you can see in the following image:

Once our C code executes, you will first see the command-line argument count, which in our case was “5” since we provided four command-line arguments. After that, the contents of the “argv[]” array will be displayed on the terminal. The first index will refer to the name of the file followed by the other indexes that will contain the values of the provided command-line arguments, which were strings in this case.

Now, we will run our same C program with a different set of command-line arguments to make this point clear that it is not mandatory to provide the string arguments only as of the command-line arguments. This time, we have executed our program with integer values in the manner shown below:

$ ./CLA Integer1 Integer2 Integer3 …

After the name of your object file, you can provide as many integer arguments as you want. We did the same, as you can see in the following image:

Once our C code executes, you will first see the command-line argument count, which was again “5” since we provided four command-line arguments. After that, the contents of the “argv[]” array will be displayed on the terminal. The first index will refer to the name of the file followed by the other indexes that will contain the values of the provided command-line arguments, which were integers in this case.

Conclusion

This guide taught you the basics of command-line arguments processing in C. By looking at the example provided in this article, you can quickly grasp the concept of passing the arguments at runtime through the terminal in Linux.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.