Question about the command line compilation in C ++ & java

zhaozj2021-02-16  38

This article describes relevant knowledge about the compilation of command lines in C / C and Java.

In actual programming, sometimes the main () belt parameters are met. I remember that when I first generated a console program in Borlan C Builder, the program automatically generated a parameter-based main ():

Void main (int Argc, char * argv [])

At that time, I was very strange, now I understand that the program adds some processing information to the program through the parameter of the main () function, allowing the program and the user to implement more interactive communication. If your program needs to be compiled by the command line parameter, then the source program The main function main () requires a parameter - that is, the so-called command line parameters. Main () function head format with parameters:

Void Main (int Argc, char * argv []) or void main (int argc, char ** argv)

In fact, Argc and Argv can also be written into other legal identifiers (Identifier), I will follow the general situation here.

Let's take a look at the specific meaning of these two parameters:

The first parameter argc is an INT type, which is used to store the number of command line parameters. In fact, the value stored by the argc is more than the number of command line parameters. The command word (executable file name) is also calculated. Inside.

The second parameter Argv is a one-dimensional first-level pointer array, which is a string of the various parameters and command words in the command line, and specifies:

Argv [0] Store command word

Argv [1] Store the first parameter in the command line

Argv [2] Store the second parameters in the command line

...

Here, the value of Argc and the value of each element is the system automatic group assignment.

The main () function that is described here is actually a specific example of the pointer array application.

Below is a specific example of a command line parameter compilation (VC 6.0 debugging):

//test.cpp

#include

Void main (int Argc, char * argv [])

{

COUT << "The number of command line arguments is:" << argc << endl;

Cout << "The program name is:" << argv [0] << ENDL;

IF (Argc> 1)

{COUT << "The command line arguments: / n";

For (int i = 1; i

COUT << argv [i] << endl;

}

The above source file generates an executable file Test.exe after compiling. Then in the VC, we will set the following: in Project -> Setting -> Debug -> Program Arguments arbitrary set command line parameters (casually enter ):

This is the first time I post documents here. ^ _ ^

Then run, see what you will get ... I have analyzed it ...

Command lines in C and the command lines in Java compile some small differences, for the Java program with public static void main (string args []) as a function portal, args [0] puts the first parameter in the command line, Arg [1] Store the second parameter in the command line ... This is different from the C / C language. Also, there is no corresponding specified parameter in Java to store the number of command lines (anyway I have not found), There is ARGC in C / C. Here is a topic of a Java programmers authentication on the command line to compile:

//Test_87.java

Public class test_87 {

Public static void main (String [] args) {

String foo = args [1]; string bar = args [2];

String baz = args [3];

System.out.println ("baz =" baz);

}

}

Post-compilation command: Java Test_87 1 2 3 4

The result of the screen output is:

Baz = 4

(JDK1.4 is debugged.)

Please give me a lot of advice.

转载请注明原文地址:https://www.9cbs.com/read-26269.html

New Post(0)