Contacts

C Use of the Main argument. Optional and named arguments. Debugging of the program with command line arguments

Tags: Command Line Settings

Command Line Settings

C and - compiled language. After assembly, the program is an executable file (we do not consider the creation of dynamic libraries, drivers, etc.). Our programs are very simple and do not contain runtime libraries (Runtime Libraries), so they can be transferred to a computer with the same operating system (and similar architecture) and are running there.

The program during startup can take parameters. They are the arguments of the Main function. General View of Main Function Next

Void Main (int argc, char ** argv) (...)

The first ArGC argument is the number of transmitted parameter functions. The second argument is an array of strings - the parameters itself are actually. Since the parameters of the function can be any, then they are transmitted as strings, and the program itself must disassemble them and lead to the desired type.

The first argument (ARGV) is always the name of the program. In this case, the name is displayed depending on where the program was running.

#Include. #Include. Void Main (int argc, char ** argv) (PrintF ("% s", argv);)

Now learn to work a little with the command line. It will be needed in order to transmit the arguments to our program. The combination of the Win + R keys calls the "Run" window. Dial in it CMD and you will open command line. You can also find cmd.exe search in the Start menu. In unix-like operating systemsoh, you can call the program "Terminal".

We will not study any many teams. Only those that will be needed in the work.

Standard for all operating systems The CD command transforms to the desired folder. There are two reserved name -. (Point) and .. (two points). The point is the name of the current folder.

Does not go anywhere

Appeal to the parent folder

Transition to the parent folder

For the transition to the desired CD address is written. For example, you need to go to Windows to the folder C: \\ Windows \\ System32

CD C: \\ Windows \\ System32

In Linux if you need to go to the / Var / MySQL folder

CD / VAR / MYSQL

If the path contains spaces, it is written in double quotes

CD "D: \\ Docunts and Settings \\ Prolog"

The terminal has the following useful features: if you press the up arrow, the previous executed command will appear. If you press the Tab, the terminal will try to add a string to the command known to it, or add the path, turning out all the folders and files in the current folder.
Dial CD C: \\
Press TAB and see what is happening.

One more important team DIR on Windows and Ls on Linux, displays the contents of the current folder to the console (the folder in which you are currently)

Your program returned your full name. Go to the folder where your program is located and see its contents.


Now, after we switched to our folder, you can perform our program. To do this, type her name.


Note - the name has changed. Since the program is called from its folder, it is displayed relative to the name. Now you will change the program and make it takes out all the arguments. who are transferred to her.

#Include. #Include. Void Main (int argc, char ** argv) (int i; for (i \u003d 0; i< argc; i++) { printf("%s\n", argv[i]); } }

Collect the project. Before assembly, make sure that the program is closed. Now call the program by passing it different arguments. To do this, write the name of the program and through the argument space


Let's now write a program that receives two arguments of the number and displays their sum

#Include. #Include. #Include. Void Main (int argc, char ** argv) (int a, b; if (argc! \u003d 3) (PrintF ("Error: Found% D Argments. Needs Exactly 2", ArGC-1); EXIT (1); ) a \u003d ATOI (argv); b \u003d atoi (argv); printf ("% d", a + b);)

We collect and call


Thus, most programs work. Click on a label, you call the program to which it refers. Most programs also take different arguments. For example, you can call browser Firefox. From the command line and transfer arguments
Firefox.exe "www.mozilla.org" site "and he will immediately open the sites in two tabs specified addresses.

Many standard commands also have parameters. It is accepted in Windows that they start with a straight look, in Unix with a minus or two minuses. for example

Displays only folders, and in the Linux terminal

LS -L displays all files and folders with attributes

For viewing additional windows commands Dial on the HELP command line or see the guide (it is easy to find on the Internet). For Linux teams and their options, much more, and some of them are independent programming languages, so it is worth learn at least a minimum set and their options.

It happens that the data in the program is transmitted from the command line when it is called. Such data is called command-line arguments. It looks like this, for example:

./a.out Test.txt LS -LT / HOME / PETER /

Here are the programs a.out (from the current directory) and Ls (from one directory specified in the PATH environment variable). The first program from the command line receives one word - test.txt, the second is two: -lt and / home / peter /.

If the program is written in C, then when it starts, the control is immediately transmitted to the Main () function, therefore, it is it that gets the command line arguments that are assigned to its variables.

Prior to this, we defined the Main () function as if it did not accept any parameters and returns nothing. In fact, in the language C, any default feature (if nothing else is defined) returns an integer. You can make sure that you can make sure. If you write the code in this way:

main () (PrintF ("Hi \\ n "); Return 0; )

That no warning or error in compiling will arise. The same will be, if you record Int Main (). This proves that the default function returns an integer, and not nothing (void). Although what returns the function can always be "override", for example, voidmain () or Float main ().

When calling a program from the command line, a pair of data is always transmitted:

  1. integerdenoting the number of words (elements separated by spaces) on the command line when calling,
  2. signpost on an array of stringsWhere each row is a separate word from the command line.

It should be borne in mind that the program name itself is also considered. For example, if the call looks like this:

./a.out 12 Theme 2

The first argument of the program is 4, and the string array is defined as ("./a.out", "12", "Theme", "2").

Pay attention to the terminology, there are only two program arguments (number and array), but any arguments of the command line. The command line arguments are "converted" into the program arguments (in the Main () function arguments).
This data (number and pointer) are transmitted to the program even when it is simply called by name without transferring something to it :./a.out. In this case, the first argument has a value of 1, and the second indicates an array consisting of only one line ("./a.out").

The fact that the program is transmitted to the program does not mean that the MAIN () function must receive them. If the Main () function is defined without parameters, it is impossible to access the command line arguments. Although nothing bothers you to transfer them. Errors will not arise.

To access the data transferred to the program, they must be assigned to variables. Since the arguments are immediately transmitted to Main (), its title should look like:
Main (Int N, Char * Arr)

The first variable (n) contains the number of words, and in the second pointer to the row array. Often the second parameter is written as ** ARR. However, this is the same. Recall that the array itself lines, contains pointers to lines as its elements. And in the function we transmit a pointer to the first element of the array. It turns out that we transmit a pointer to the pointer, i.e. ** ARR.

The task
Write such a program:

#Include. INT MAIN (int argc, char ** argv) (int i; printf ("% d \\ n ", argc); for (i \u003d 0; i< argc; i++ ) puts (argv[ i] ) ; }

It displays the number of words on the command line when it is called and every word with new String. Call it without command line arguments and with arguments.

In the program, we used the ARGC and ARGV variables. It is common to use such names, but in fact they can be anyhow. It is better to adhere to this standard so that your programs are more understandable not only to you, but also to other programmers.

The practical value of data transfer to the program

If you have experience in the GNU / Linux command prompt, you know that most teams have keys and arguments. For example, when viewing the contents of directories, copying, moving as arguments, objects are indicated file SystemOver which the team is performed. The features of its execution are determined using keys. For example, in the team

CP -R ../les_1 ../les_101

cP is the name of the command, -r - the key, and ../ les_1 and ../ les_101 - command arguments.

In general, the addresses of files and "modifiers" (these are the keys) of the program process are transmitted in the program.

We will write a program that opens the files specified by the user on the command line to record or add and writes (adds) to one and the same information that the user enters from the keyboard during the program execution process:

#Include. #Include. Main (int argc, char ** argv) (int i, ch; file * f [5]; if (argc< 3 || argc > 7) (Puts ( "Invalid number of parameters"); Return 1; ) if (STRCMP (argv [1], "-w")! \u003d 0 && strcmp (argv [1], "-a")! \u003d 0) (Puts ( "The first parameter can be either -w, or -a"); Return 2; ) for (i \u003d 0; i< argc- 2 ; i++ ) { f[ i] = fopen (argv[ i+ 2 ] , argv[ 1 ] + 1 ) ; if (f[ i] == NULL) { printf ("File% s cannot be opened \\ n", argv [i + 2]); Return 3; )) While ((CH \u003d getchar ())! \u003d EOF) for (i \u003d 0; I< argc- 2 ; i++ ) putc (ch, f[ i] ) ; for (i= 0 ; i < argc- 2 ; i++ ) fclose (f[ i] ) ; return 0 ; }

Explanation to the code:

  1. An array of five file pointers is created. Therefore, you can simultaneously open no more than five files. The file pointer of the first file will be stored in the element of the F, the second - in F, etc.
  2. The number of command line arguments is checked. They should be at least three, because The first is the name of the program, the second - the opening mode of the file, the third one or the only file in which the record will be made. Since the program allows you to open only five files, the total number of command line arguments cannot be more seven. Therefore, if the number of arguments is less than 3 or more than 7, then the program is completed, because The RETURN operator leads to the output from the function, even if there is still code after it. The value returned from the function is unequal 0, can be interpreted by the parent process, as a message that the program completed with an error.
  3. Checked the correctness of the second command line argument. If it is not equal to any "-w", nor "-a", then the conditional expression in the second IF returns 1 (true). The STRCMP () function allows you to compare strings and returns 0 if they are equal.
  4. The Forcycle loop opens the files at the specified addresses, which begin with the third element of the argv array. That is why 2 is added to I, to receive the elements of the argv array, starting with the third. The expression ARGC-2 indicates the number of file names passed; Because The ArGC stores the total number of command line arguments, the first two of which are not file names.
  5. Expression ARGV + 1 allows you to "cut" from the "-W" string (or "-a") substring "W" (or "A"), because ARGV is essentially a pointer to the first element of the string. Adding to the pointer unit, we shift it to the next element of the array.
  6. If the file fails, the FOPEN () function returns NULL. In this case, the program is completed.
  7. Each character entered by the user from the keyboard is written to all open files.
  8. At the end, the files are closed.

Optional and named arguments

Optional arguments

In the C # 4.0 version, a new tool enhancing the convenience of specifying arguments when calling a method. This tool is called optional arguments And allows you to determine the default value for the method parameter. This value will be used by default if the corresponding argument is specified for the parameter when the method is called. Consequently, specify the argument for such a parameter is not necessary. Optional arguments allow you to simplify the call to the methods where the default arguments are applied to some parameters. They can also be used as a "abbreviated" form of overload methods.

The main stimulus to add optional arguments was the need to simplify interaction with COM facilities. In several Microsoft object models (for example, Microsoft Office.) Functionality is provided through COM facilities, many of which were written long ago and are designed to use optional parameters.

An example of using optional arguments is shown below:

Using SYSTEM; using system.collections.Genic; Using System.linq; Using System.Text; Namespace ConsoleApplication1 (Class Program (// Arguments b and with specify when calling optionally static int mysum (int a, int b \u003d 5, int c \u003d 10) (RETURN A + B + C;) Static Void Main () (int sum1 \u003d mysum (3); int sum2 \u003d mysum (3,12); console.writeline ("sum1 \u003d" + sum1); console.writeline ("Sum2 \u003d" + Sum2); Console.ReadLine ();)))

It should be borne in mind that all optional arguments must certainly be indicated to the right of mandatory. In addition to the methods, optional arguments can be used in designers, indexers and delegates.

The advantage of optional arguments is, in particular, in the fact that they simplify the programming treatment with complex challenges of methods and constructors. Indeed, often in the method you have to set more parameters than usual. And in such cases, some of these parameters can be made optional due to the accurate use of optional arguments. This means that only those arguments that are important in this particular case are needed, and not all arguments that otherwise must be mandatory. This approach allows us to rationalize the method and simplify the programming appeal to it.

Named arguments

One more functionalitywhich has been added to C # with the output version of the version .NET 4.0, is the support of the so-called named Arguments (Named Arguments). As is known, when transferring arguments, the procedure for their follows, as a rule, should coincide with that order in which the parameters are defined in the method itself. In other words, the value of the argument is assigned to the parameter by its position in the list of arguments.

This restriction is designed to overcome the named arguments. The named argument allows you to specify the name of the parameter to which its value is assigned. And in this case, the order of the arguments no longer matter. Thus, named arguments to some extent similar to those mentioned earlier initializers of objects, although they differ from them with their syntax. To specify the name argument, the next form of syntax is used:

parameter name: Value

Here parameter name Indicates the name of the parameter to which the value is passed. Of course, the parameter_name must signify the name of the valid parameter for the called method.

While creating console application In the C ++ programming language, the string is automatically created very similar to this:

Int Main (int argc, char * argv) // Main () function parameters

This string is a header main function main (), the parameters of ARGS and ARGV are announced in the brackets. So, if you run the program through the command line, then it is possible to transfer any information to this program, for this, and exist ARGC and ARGV parameters. The ARGC parameter has an INT data type, and contains the number of parameters transmitted to the Main function. Moreover, ARGC is always at least 1, even when we do not transmit any information, since the first parameter is the name of the function. ARGV parameter is an array of pointers to strings. Through the command line, only a string type can be transmitted. Pointers and lines are two big topics that are created separate sections. So it is through the ARGV parameter and any information is transmitted. We will develop a program that we will run through the command windows string, and transmit some information to it.

// argc_argv.cpp: Determines the input point for the console application. #Include "stdafx.h" #include Using Namespace STD; Int Main (Int Argc, Char * ArgV) (if (argc\u003e<< argv<

// Code Code :: Blocks

// DEV-C ++ code

// argc_argv.cpp: Determines the input point for the console application. #Include. Using Namespace STD; INT MAIN (int argc, char * argv) (if (argc\u003e 1) // If we transmit arguments, the ArGC will be greater than 1 (depending on the number of arguments) (cout<< argv<

After you have shyled the program, open the command line of Windows and drag into the command line window of our program in the command line window, the full path to the program will be displayed on the command line (but you can prescribe the path to the manual program), then you can click ENTER And the program will start (see Figure 1).

Figure 1 - Main function settings

Since we just launched the program and did not pass any arguments, the message not arguments appeared. Figure 2 shows the start of the same program through the command line, but already with the transmission of the Open argument.

Figure 2 - Main function parameters

The argument is the word Open, as can be seen from the figure, this word appeared on the screen. You can transmit several parameters immediately, separating them with each other. If you need to pass the parameter consisting of several words, then they must be taken into double quotes, and then these words will be considered as one parameter. For example, the figure shows the launch of the program, with the transfer of the argument consisting of two words - it work.

Figure 3 - Main function parameters

And if you remove quotes. I will see only the word IT. If you do not plan to transmit any information when you start the program, you can delete arguments in the Main () function, you can also change the names of the arguments. Sometimes there is a modification of ARGC and ARGV parameters, but it all depends on the type of application being created or from the development environment.

In the C program, you can transfer some arguments. When the computation at the initial computation is made to the main (), it is transmitted three parameters. The first of them defines the number of command arguments when accessing the program. The second is an array of pointers to symbolic strings containing these arguments (one argument one in one line). The third is also an array of pointers to character strings, it is used to access the operating system parameters (to environment variables).

Any such string seems to be:

variable \u003d value \\ 0

The last string can be found in two final zeros.

We call the arguments of the Main () function, respectively: ARGC, ARGV and ENV (any other names are possible). Then the following descriptions are allowed:

main (int argc, char * argv)

main (Int Argc, Char * Argv, Char * ENV)

Suppose that on the disk A: there is some prog.exe program. Turn to it as follows:

A: \\\u003e Prog.exe File1 File2 File3

Then argv is a pointer to a string A: \\ Prog.exe, ArgV - on the line File1, etc. The first actual argument indicates Argv, and on the last - ARGV. If argc \u003d 1, then after the program name, there is no parameter command prompt. In our example argc \u003d 4.

Recursion

Recursion is called such a call method, in which the function addresses itself.

An important point when drawing up a recursive program is the organization of exit. It is easy to make an error that consists in the fact that the function will consistently cause itself infinitely long. Therefore, the recursive process should step by step to simplify the task so that in the end it is not a recursive solution. The use of recursion is not always desirable, as this can lead to stack overflow.

Library functions

In the programming systems, the subprogramme to solve common tasks are combined into libraries. These tasks include: Calculating mathematical functions, enter / output data, row processing, interaction with the tools of the operating system, etc. The use of library subroutines eliminates the user from the need to develop appropriate funds and provides it with an additional service. The functions included in the library are shipped with the programming system. Their ads are given in * .h files (these are so-called included or header files). Therefore, as already mentioned above, at the beginning of the program with library functions should be strings of the form:

#Include.<включаемый_файл_типа_h>

For example:

#Include.

There are also means to expand and create new libraries with user programs.

For global variables, a fixed place is assigned in memory at all time of the program. Local variables are stored in the stack. Between them is a memory area for dynamic distribution.

The malloc () and free () functions are used to dynamically distribute free memory. The malloc () feature highlights the memory, the FREE () function frees it. The prototypes of these functions are stored in the header file stdlib.h and have the form:

void * Malloc (Size_T Size);

void * FREE (Void * P);

The malloc () function returns a void indicator; For proper use, the function value must be converted to the pointer to the appropriate type. Upon successful execution, the function returns a pointer to the first byte of the free size of size of SIZE. If there is no sufficient amount of memory, the value is 0. To determine the number of bytes required for the variable, use the SizeOF operation ().

An example of using these functions:

#Include.

#Include.

p \u003d (int *) malloc (100 * SizeOF (int)); / * Memory allocation for 100

integers * /

printF ("not enough memory \\ n");

for (i \u003d 0; i< 100; ++i) *(p+i) = i; /* Использование памяти */

for (i \u003d 0; i< 100; ++i) printf("%d", *(p++));

fREE (P); / * Release of memory * /

Before using the pointer returned by malloc (), you must make sure that the memory is enough (the pointer is not zero).

Preprocessor

Preprocessor SI is a program that processes the input for the compiler. The preprocessor browsing the source program and performs the following actions: Connects the specified files to it, makes substitutions, and also controls the compilation conditions. For preprocessor, the rows of the program starting with the symbol # are designed. One line is allowed to record only one command (preprocessor directive).

Directive

#define substitution identifier

causes a replacement in the following text of the program of the named identifier to the text of the substitution (pay attention to the lack of a point with a comma at the end of this command). Essentially, this directive enters macro definition (macro), where the "identifier" is the name of the macro definition, and the "substitution" is a sequence of characters to which the preprocessor replaces the specified name when it finds it in the program text. The name of the macro definition is taken by capital letters.

Consider examples:

The first line calls the replacement in the MAX identifier program to the constant 25. The second allows you to use in the text instead of the opening brace (() the word Begin.

We note that since the preprocessor does not check the compatibility between the symbolic names of the macro definitions and the context in which they are used, it is recommended that such identifiers are recommended to determine the #define directive, but using the keyword const with an explicit type of type (this is more related to C + +):

const int max \u003d 25;

(The int type can not specify, as it is installed by default).

If the #Define directive is:

#Define identifier (identifier, ..., identifier) \u200b\u200bsubstitution

moreover, there is no space between the first identifier and the opening bracket, this is the definition of macro-arguments with arguments. For example, after the appearance of a string of the form:

#Define Read (Val) Scanf ("% d", & Val)

rEAD (Y) operator; perceived just like SCANF ("% D", & Y);. Here Val is an argument and macro-argument with the argument.

If there are long definitions in the substitution on the next line, at the end of the next row, the symbol \\ is set.

In the macro definition, you can put objects separated by ## characters, for example:

#Define PR (x, y) x ## y

After that, PR (A, 3) will cause substitution A3. Or, for example, macro definition

#Define Z (A, B, C, D) A (B ## C ## D)

will be replaced by z (sin, x, +, y) on sin (x + y).

The symbol # placed in front of the macroartum indicates the conversion of it into the string. For example, after the directive

#Define Prim (VAR) Printf (#var "\u003d% d", var)

next fragment of the text of the program

converted so:

printf ("year" "\u003d% d", year);

We describe other preprocessor directives. The #include directive has already met earlier. It can be used in two forms:

#InClude "File Name"

#Include.<имя файла>

The action of both commands is reduced to enable files with the specified name. The first of them downloads a file from the current or specified as a catalog prefix. The second command is searching for a file in standard locations defined in the programming system. If the file whose name is recorded in double quotes, not found in the specified directory, the search will be continued in subdirectories specified for the #include command<...>. #Include directives can be invested in each other.

The following group of directives allows you to selectively compile parts of the program. This process is called a conditional compilation. This group includes #if, #ELSE, #elif, #endif, #ifdef, #ifndef directives. The main form of the recording of the #if Directive is:

#if Constant_Construction Sequence_PERATORS

Here the value of the constant expression is checked. If it is true, the specified sequence of operators is performed, and if it is false, this sequence of operators is skipped.

The action of the #ELSE directive is similar to the action of the ELSE command in S S, for example:

#if Constant) Employment

sequence_ operators_2.

Here, if the constant expression is true, then the sequencer sequencers_1 is performed, and if falsely - sequence_2.

#Elif directive means type "ELSE IF". The main form of its use has the form:

#if Constant) Employment

sequence_ operators

#elif Constant_Mapsion_1

sequence_PERATOR_1.

#elif Constant_Nesome_n.

sequence_PERATOR_N.

This form is similar to the design of the SI type: if ... ELSE if ... Else IF ...

Directive

#ifdef identifier

sets the specified identifier at the moment, i.e. Whether he entered the #Define species directive. Row of type

#ifndef identifier

checks whether the specified identifier is currently at the moment. For any of these directives, an arbitrary number of text strings may be followed by the #ELSE (#elif cannot be used) and ending with the #Endif string. If the test condition is true, then all the rows between #ELSE and #Endif are ignored, and if falsely, then the rows between checking and #else (if #ELSE words are not, then #endif). #If and #ifndef directives can "enable" one to another.

Directive view

#Undef identifier

leads to the fact that the specified identifier begins to be considered uncertain, i.e. not subject to replacement.

Consider examples. Three following directives:

check whether the WRITE identifier is defined (i.e. there was a team of the species #define Write ...), and if so, then the name of Write begins to be considered uncertain, i.e. not subject to replacement.

Directive

#Define Write Fprintf.

check whether the WRITE identifier is uncertain, and if so, then the WRITE identifier will be determined instead of the FPRINTF name.

The #error directive is written in the following form:

#Error message_AB_Obkka

If it is found in the text of the program, the compilation stops and an error message is displayed on the display screen. This command is mainly applied at the debug stage. Note that the error message does not need to enter into double quotes.

The #Line directive is designed to change the values \u200b\u200bof variables _line_ and _file_ defined in the SI programming system. The variable _Line_ contains the number of the program string performed at the current time. The _File_ identifier is a pointer to a string with the name of the compiled program. The #Line directive is written as follows:

#Line number "File Name"

Here the number is any positive integer that will be assigned to the variable _line_, the name_name is an optional parameter that overrides the value _File_.

The #Pragma directive allows you to transfer some instructions to the compiler. For example, string

it suggests that the program in the SI language has lines in the assembler language. For example:

Consider some global identifiers or macroenases (macro definition names). Five such names are defined: _line_, _file_, _date_, _time_, _stdc_. Two of them (_line_ and _file_) were already described above. The _date_ identifier defines a string in which the data of the transmission of the source file is saved to the object code. The _Time_ identifier sets the string that saves the transmission time of the source file to the object code. Macro _STDC_ is 1, if used standard - specific macrospace. Otherwise, this variable will not be determined.



Did you like the article? Share it