Command Line Parameters in the C language

C

CLI is an acronym for command-line interface or command line interpreter and represents a mechanism for interacting with a computer, operating system or software by introducing sequential, line-by-line commands.
This interface, which is based only on text input, contrasts with graphical user interface (GUI) interactions, which use mouse devices to select options or menus.
Entering commands via CLI: The system expects the user to enter a command line and confirm it by pressing the “Enter” key on the keyboard. The Command-Line Interpreter program receives analyzes (parses) and executes the required command. After execution, the program presents the user with the result, also in the form of text lines.
In order to control the execution of a program, it is often desirable to provide work data before the execution of the program and then executed without user intervention (the so-called “batch mode”).
This is done via the command line parameters. A good example is the launch of the GCC compiler on the command line with various arguments that tell her how to compile.
From the user’s point of view, command line parameters are simple arguments that are added after the name of a program in the command line when it is run.
The elements of this list of arguments are separate strings of spaces. Arguments containing spaces can be combined into one argument by closing it in quotes. Shell is the one responsible for parsing the command line and creating the list of arguments.

Example of calling a program with arguments in the command line:
gcc -Wall -I / usr / include / sys -DDEBUG -o “My Shell” myshell.c

In this case, the arguments of the command line are in this case:

– GCC
– Wall
– I/usr/include/sys
– DDEBUG
– a
– My Shell
– myshell.c

From the programming point of view, the parameters of the command line are accessible by using the parameters of the main () function. Thus, when using command line arguments, the main () function will be defined as follows:
int main (int argc, char * argv [])

Thus, the main () function formally receives two parameters, a whole and a character strings vector. The names of the two variables do not have to be argz and argv, but their type, yes.

Their meaning is as follows:
Argument – represents the number of command-line parameters. As shown in the previous example, there is at least one parameter, which is the name of the program itself. The name that was used to launch the program – and which may be different from the executable name – for example by creating a symlink in Linux .
char * argv [] (arguments value) – represents a vector of strings with argc elements (indexed from 0 to argc – 1). Always argv [0] contains the program name.
The first argc parameter is an integer that counts the number of arguments available on the command line. This value can be at least 1 because the program name is counted as the first argument. The argv [] massif is a massive pointer to the strings. As for any massive first index is 0, the last being argc-1. The pointer argv [0] names the name of the program name (including the path), argv [1] points to the first argument after the program name, argv [2] on the second, and so on. note that the argc and argv [] names are not obligatory, any valid names in C. may be used.

Command-line parameters can be accessed through the argv vector and can be processed with standard string handling functions.

Recent Posts

Archives

Categories