[Post] use of int main (INT argc, char * argv []) in Windows

Source: Internet
Author: User
[Post] use of int main (INT argc, char * argv []) in Windows

Generally, for beginners of C language, our main function has no form parameters. So what is the problem?

According to the C language, the main function can have only two parameters. Normally, these two parameters are argc and argv. The format is as follows:

 

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

{

//

Program

//

}

Resolution: The main function has two parameters. The first argc is an int integer. The second is the pointer array. That is, this parameter is an array, and the members of this array are pointers. Its Pointer Points to the char type.

Argc records that the user is runningProgramNumber of input parameters in the command line.

Arg [] points to at least one character pointer in the array, that is, Arg [0]. It usually points to the executable file name in the program. In some versions of compilers, programs are also included.
File Path.

This sentence is too general for our classmates to understand.

 

Yes.

When calling an executable program, parameters must be passed to the program in some cases. For example, you can click notepad.exe on the console,
Press enter to run the Notepad program. If you want to open a notepad file at the same time when you open notepad.exe, you can
The name of the file, for example, notepad.exe example.txt (the file is in the current path ).

How can we obtain these input parameters in the program? This is done by the compiler. the compiler will input the parameter information.
In the parameter list of the main function.

The parameter list of the main function stores the input parameter information. The first parameter argc records the number of input parameters,
The second parameter is a string array. Each unit of the string array is of the char * type and points to a C-style string.
Take notepad.exe example.txt as an example.
Argc is 2, which means there are two valid units in the argv array.
The first unit points to the string "notepad.exe"
The second unit points to the string "example.txt"

The first unit in the argv array points to a string that is always the name of the executable program. The subsequent unit points to a string that is the parameter used for Program Calling in turn.

The assignment process is completed by the compiler. We only need to read the data.
-----------------------------------------------------------------------------

Below is an instanceCode: C language verification.

# Include <stdio. h>
Int main (INT argc, char * argv [])
{
Int I;
Printf ("% d", argc );
For (I = 0; I <argc; I ++)
{
Printf ("% s \ n", argv [I]);
}
Scanf ("& I ");

}

After compilation and connection, the program becomes executable: main.exe.

You can start-> Run-> input cmd. The window appears.

 

Access the directory where main.exe is located through the "cdns" command. Enter main Chen Hui Jie and press enter to view the results.

The following describes another method to verify the signature:

Through powerful running

 

Enter cmd in the text. Open the console because there are

In the same way, we copy and paste main.exe to the c: \ windows \ system32 path. You can easily enter the main [real parameter]... You can.

For example:

The result is as follows:

 

This post is introduced from the http://hi.baidu.com/grandyang/item/907649c064cc152c47d5c00a

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.