Command Line compilation in C ++ & JAVA

Source: Internet
Author: User

Command Line compilation in C ++ & Java

This article describes how to compile command lines in C ++/C and Java.

In actual programming, the main () scenario with parameters is sometimes encountered. I remember the first time I generated a console program in borlan C ++ builder,
The program automatically generates a main () with parameters ():

Void main (INT argc, char * argv [])

It was strange at the time,
Now I understand that the main () function parameter can be used to add some processing information to the program, so that the program and the user can implement more interactive communication.
If your program needs to compile the command line parameters, the main function main () of the source program needs to contain parameters-the so-called command line parameters. The format of the main () function header with parameters is:

Void main (INT argc, char * argv [])
Or void main (INT argc, char ** argv)

In fact, argc and argv can also be written as other valid identifiers. Here I follow the general situation.

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

The first parameter, argc, is of the int type. It is used to store the number of command line parameters. In fact, argc stores 1 more value than the number of command line parameters, the command word (Executable File name) is also included.

The second parameter argv is a one-dimensional array of first-level pointers. It is used to store strings of various parameters and command words in the command line, and stipulates:

Argv [0] Stores command words

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

Argv [2] stores the second parameter in the command line

...

Here, the value of argc and the value of each element of argv [] are automatically assigned by the system group.

Here, the main () function with parameters is actually a specific instance applied to the pointer array.

The following is an example of how to compile command line parameters (through debugging in VC ++ 6.0 ):

// Test. cpp

# Include <iostream. h>

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 <argc; I ++)

Cout <argv [I] <endl;

}}

After the source file is compiled and connected, the executable file test.exe is generated.
Then in VC,
Make the following settings:
In Project --> Setting --> Debug --> Program Arguments, you can set the command line parameters (whatever you enter ):

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

Run the command to see what results you will get ...... Analyze it by yourself...

 

The command line compilation in C ++ is slightly different from that in Java,
For Java programs that use public static void main (String args []) as the function entry, args [0]
Put 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. in addition, there is no corresponding rule in Java to specify a parameter to store the number of command lines (not found anyway), and there is argc in C ++/C. here is a question about command line compilation in Java programmer authentication:

// 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 );

}

}

Run the following command after compilation: java Test_87 1 2 3 4

The screen output result is:

Baz = 4

(JDK1.4 debugging passed .)

 

Please kindly advise.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.