Command Line Argument in C

What is Command Line Argument ?

In case of command line argument we can pass value from the command line before compiling or executing the program. In general, we input for the program after compiling the program. But here we provide all the inputs before compiling the program. Now here is an example, let’s check it.

In this program, we can see two things.

  1. argv

  2. argc

What is argc?

argc in Command line argument in C means the total number of arguments passed through command line argument.

If we don’t pass any argument in command line then argc value will be 1 as the file location of the executing program will be passed and will be stored into argv[0].

Suppose, if you pass two arguments then your argc value is 3 as 2 for your arguments and 1 for the location of the executing program.

What is argv?

argv stands for argument value. argv in command line argument in C is a pointer array which points to each argument passed to the program.

argc stands for argument count and argv stands for argument values. These are variables passed to the main function when it starts executing.

Here, we check if the value of argc is equal or greater than 2, only then we will print the value of the passing arguments. You can also check the executing file location and the value of argument count.

 

Otherwise, it will show “argument list is empty” and also the location of the executing file due to printing argv[0] and the value of argument count for printing argc.

In this program, there are different data types of data are passed using command line argument. But all the taken as string input. So do the specific data type action we need to convert the data type in that specific format from the string input.

The sscanf() function allows us to read formatted data from a string rather than standard input or keyboard. Its syntax is as follows:

Syntax: int sscanf(const char *str, const char * control_string [ arg_1, arg_2, … ]);

Recent Posts