Command Line Arguments in C – LinuxWays
In this Linux Ways article, you’ll learn what command line arguments are in the C language. In this article, we will explain what command line arguments are, how to access them, and how to use them to create the executable programs that send or receive these types of parameters.
We’ll also see how to send the arguments when launching an application from the command console. In another section, you’ll learn how to convert the command line arguments into the different data types that are used in C since this is essential to use its full potential.
Command Line arguments in the C Language
The C programming allows you to create the programs that send or receive the command line arguments. The command line arguments are parameters that are passed to a process when the execution begins. These arguments can be sent from a parent process to a child process when it starts it, or by the user when he starts the application. Sending and receiving these types of parameters is done using chains, each pointed in a vectorized array of pointers.
How to Send the Command Line Arguments from the Linux Console
In this section, we’ll show you how to run the applications and send the arguments from the Linux console.
Use the following syntax to launch an executable and send it to the command line arguments from the Linux console:
./filename.extension argument1 argument2 argument..n
The name and extension of the executable must be preceded by the “./” characters and the arguments must each be separated by a white space.
Applications that Receive the Command Line Arguments in the C Language
The command line arguments arrive at the process in string format, and the applications can access them using a vector of pointers where each pointer points to an argument. In the following, we see the prototype of the main() function which uses an application that receives the command line arguments:
int main ( int argc, char *argv[] )
The argv[] argument is the vector of an array of pointers to strings, each of which contains a received input argument.
The first argument of the array is always a string that contains the name of the executable, while the last is a pointer to NULL, marking the end of the list of arguments received. In the following, we see a table that shows how the list of command line arguments is organized in the array:
argv[0] | File name (Start of list) |
argv[1] | Argument 1 |
argv[2] | Argument 2 |
argv[n]… | Argument n… |
argv[n+1] | NULL (End of list) |
The “argc” input argument is an integer where the OS indicates the number of parameters received by the process and can be used in conditionals or loops to determine the end of the argument list.
How to Access the Command Line Arguments in the C Language
Next, we look at an example that uses the printf() function to display the received command line arguments by accessing them through the argv[] vector and an integer that acts as an index.
The code for this example consists of a “for” loop that declares the index variable and assigns it with the value of 0. The index integer is incremented by one each time the loop runs a complete cycle and has two functions. It indexes the argv[] vector and determines the loop exit condition if its value is equal to “argc”. In the following, we see the code for this example:
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
for (int index =0;index != argc; index++)
{
printf (“Argument %i: %sn“, index, argv[index]);
}
}
Now, we compile this code and save its output under the “example” name and the “.bin” extension as shown in the following:
In this way, we create the “example.bin” executable file which can accept an unspecified number of command line arguments and display them on the screen.
Now, we run the application and send its arguments from the command console. The following image shows the execution of the application with the arguments sent:
How to Convert the Command Line Arguments in the C Language
As we already seen, command line arguments are sent and received only in the form of strings. So, if we want to work with data of a different type, we have to make conversions.
In this section, we will show you how to convert the received strings into numeric values and store them in variables of type int, double, long, and long long. To do this, the C language provides a number of functions that convert the numeric values of strings into different data types. Next, let’s look at a table of functions that are defined in the “stdlib.h” header that are used to convert the numeric values from ASCII strings into different data types:
Function | Description |
atoi() | Converts a string to an integer |
atol() | Converts a string to a long |
atoll() | Converts a string to a long long |
atof() | Converts a string to a double |
This family of functions converts the string that is pointed to in its input argument to its numeric value and returns it in a data type int, long, long long, or double.
In the following, we can see an example where we use the atoi() function to convert the second and third arguments received to an integer and we store the converted value in the variables “a” and “b” of type int. Then, we perform a sum operation between the two variables and display the result on the screen:
In the following, we will see the execution of the application with the arguments converted to integers and displayed on the screen:
Applications with Sending Command Line Arguments in C
As we explained previously, command line arguments are sent when an application is started. In the C language, there are several methods to start the applications from the code and sending arguments. In the following examples, we will look at the two most practical methods available in C for this purpose.
Method 1:
The most common method is to duplicate a process with the fork() function and then replace it with the executable that we want to run with the execv() function. This way, we open a child process to which we send the command line arguments. We will see the prototype of the execv() function in the following:
int execv(const char *pathname, char *const argv[]);
As we see in the prototype, the execv() function also uses the argv[] vector as an input argument. This function replaces the calling process with the executable that is specified by its absolute path, name, and extension in pathname, and sends it the strings that contain the command line arguments, each pointed by its pointer in the array argv[].
Next, let’s look at an example where we use the fork() function to create a child process which we then replace with the “example.bin” executable that we used in the first example. In the following code, we see the declaration of the array of pointers, the assignment of the strings to the command line arguments, and the call to the execv() function:
#include <stdlib.h>
#include <unistd.h>
void main()
{
if (fork() ==0)
{printf (“Child PID: %in“, getpid());
char *Ptr_arg[5];
Ptr_arg[0] = “example.bin”;
Ptr_arg[1] = “Hello”;
Ptr_arg[2] = “World “;
Ptr_arg[3] = “Hello “;
Ptr_arg[4] = “Linux Ways”;
Ptr_arg[5] = NULL;
execv (“/home/linuxways/example.bin”, Ptr_arg);
}
else
{
printf (“Parent PID: %in“, getpid());
sleep(5);
}
}
The following image shows the execution of this code:
As we can see, this code opens the “example.bin” executable which is identified by PID 11329, and passes it the command line arguments that are assigned to the “Ptr_arg” pointer array.
Method 2:
The second method is to start an executable and pass it with the command line arguments using the system() function. We will see the prototype of this function as follows:
This syscall executes in the system the command that is sent in the command input argument. So, to launch an executable or run the code with this function, we must use the following syntax:
The following code uses the system() function to execute the “example.c” code which belongs to the “example.bin” executable file that we saw in the first example of this article, and passes it with two command-line arguments:
#include <stdlib.h>
#include <unistd.h>
void main()
{
system (“./example.c Hello World”);
}
The following figure shows how this code starts the execution of “example.c” which accesses the received arguments and displays them in the console:
Conclusion
In this Linux Ways article, we just showed you what the command line arguments are in the C language. We learned what they are used for, what format they are in, and how the argv[] vector is used.
To make sure that you know everything about this topic, we discussed how to create and send the applications that receive the command line arguments, as well as execute them and send the arguments from the command console.
I am the Founder of Clipping Path Creative Ltd (Outsource Photo Editing Company) based in the UK. Also, I am a professional WordPress expert and working on Fiverr since 2016. I am offering all kinds of WordPress issues fix, Content upload and formatting, providing & installing SSL, Website migration, and website creation at affordable prices. Already achieved 5000+ reviews on Fiverr.