| by Arround The Web | No comments

Command Line Argument Processing in C

When we have a program to run, the command line arguments could be the arguments declared in the system’s command line preceding the program’s name. Our program will accept the types of arguments to be passed from the command line after the program’s execution. In the command line argument, we have two main parameters, which are “argc” and “argv”. The “argc” is the argument count, whereas “argv” is the argument vector in the C language. The parameter argument count indicates how many arguments were entered in the command line while writing the program. In context, the argument vector parameter is an array of pointers of the characters of the object in the C programming language for command line argument. The parameter argument count border is frequently more notable than or equal to 1.

A range of order line parameters presented by the programmers is flawed strings. Argv[0] is the order in which the show triggers the program. The main command-line assertion is argv[1].

The command-line interface credentials are used in C whenever it is essential to provide the program’s attributes to external sources, and there is no desire to use them within the code. Working framework arguments are the traits that the order line passes to the C program when the instruction is executed. Any parameter presented to the script is indicated by a pointer that is maintained within a pointer vector referenced by “argv[]”. The count of arguments passed to the program is addressed by “argc”. By using Command Line Arguments, the program to be operated can be regulated from a range rather than by hard coding the parameters inside the program.

Procedure

In this article, we will discuss the command line processing argument in the C language and how we can run any command in single or multiple lines by implementing our code in the C language.

Syntax

The syntax for the command line argument in the C language should be written along the main function with argument count in integer data type and argument vector in character data type.

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

Example # 01

Let us look at the first example of our article, which we are implementing on the tool “visual studio code” and the operating system of “Windows”. So, we included two headers first in our code that stores a lot of backend code stored in two headers. Let’s check our main function of the code where we have declared argument count as “argc” with the integer data type and argument vector taken as a pointer “argv” with character data type within the main function. Then we declared variable “b” of integer type and applied for loop on a variable which starts from “0”, and the further loop continues till it reaches the argument count. After that, we applied a simple print function and stored variable “b” in the argument vector where “%s” is used to define a string. The code ends at last after applying the return statement in our code.

#include<stdio.h>

#include<conio.h>

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

{

    int b;
    for(b=0;b<argc;b++)
    {
    printf("%s",argv[b]);
    }
    return 0;

}

Opening Run Dialogue Box

After that, we will compile our C language code and save it into the location “desktop” of our system. After saving the C file on the desktop with the name “CMDLINE.c”, we will open the run dialogue box by pressing the buttons “Windows+R” and typing “cmd” on the text bar, and then clicking “OK”.

After opening the command prompt, we entered the Windows library “cd” to get into the file location of the code. We use the “cd” command and type “desktop” because we saved our file there. Ensure that space exists between the library and the location. Now, we entered our file name “cmdline” along with “.exe” for extension and wrote our argument. After pressing enter, the command line argument is processed to our code file “CMDLINE.c”.

Example # 02

To understand the command line argument better, we have taken another example. Headers and the syntax of the main function are the same as we have seen in our previous example. We declared the variable “w” as an integer data type, and the difference is that we started our loop for variable “w” from “1” and reached to argument count. After that, we applied the print function along with “%s” for the string and used “\n” for spaces of two lines between the arguments, and we ended up with the code along with the return statement. A strict double statement mark (“) is acknowledged to be followed by the punctuation line of a backslash (\”).

After completing the coding work, we will save the file again. Here, we save it with the same name as “CMDLINE.c” on the desktop. But ensure that there must not be the previous file with the same name on the same location. After that, we could open the run dialogue box and open the command prompt for giving the arguments differently.

#include<stdio.h>

#include<conio.h>

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

{

    int w;
    for(w=1;w<argc;w++)
    {
    printf("%s\n\n",argv[w]);
    }
    return 0;

}

As we have entered the “cd” library in the previous example, we will take the same here, give one space and enter the file location “desktop”. After pressing the enter button, we will provide the file name “cmdline” with the argument message “This Is Our Second Command Line Argument in C on Windows”. After pressing enter, each argument will be separated and displayed separately after the space of two lines. This space gap between each argument happens because of using the “\n” new line notes twice in the code.

Conclusion

Our article covers the “command line argument in C” implemented in Windows. This will enable us to understand how we use command line arguments in single or separate lines. For this purpose of understanding, we have used two examples; where the first one covers the command line arguments attempted once it is just a single line with all of the arguments together. In the second example, we understand the command line argument separate from one another.

Share Button

Source: linuxhint.com

Leave a Reply