Main function--command-line arguments and environment variables

Source: Internet
Author: User

The main function, which is a program entry point for developing a platform based on C, usually shows that our program seldom pays attention to the function main function parameter list, but simply gives the following case.

int main () {System ("pause"); return 0;}

Open MSDN, view the main function,

Main (int argc, char *argv[], char *envp[])
{
Program-statements
}

First, let's take a look at the first two parameters, argc and argv[], these two are mainly used to describe the command line arguments, but not that they are command line arguments, for a program, they represent the main function of the formal parameter. (a bit around)

For example, open cmd, such as

650) this.width=650; "title=" 1.png "src=" Http://s2.51cto.com/wyfs02/M02/7F/64/wKioL1cdjQfSdsfiAACl3w4XXcE563.png " alt= "Wkiol1cdjqfsdsfiaacl3w4xxce563.png"/>

We enter Hello.exe, indicating that this is an executable program, we can keep up with some parameters,-m,2,1, etc., Parameters here have no practical significance, But if we write the hello.exe in advance, and then we add the parameters can be obtained, then we add these parameters, is called the command line parameters, and the main function parameters are different.

That can be run under CMD, how to set environment variables in the external environment?

Open vs Compiler, Project-----> Properties----> Debug

650) this.width=650; "title=" 2.png "src=" Http://s2.51cto.com/wyfs02/M02/7F/66/wKiom1cdhDix2wSeAADbk_wTWL8543.png " alt= "Wkiom1cdhdix2wseaadbk_wtwl8543.png"/>

Here, we can set the command line parameters in advance.

ARGC and argc[] are used to adjust these parameters, they can get to the command line parameters we set

Wherein, ARGC represents the number of parameters, to the program, to identify the command line arguments are recognized as a string, and argv[] is a character pointer array, each element of the array is char* type, pointing to the parameters of our command line arguments, the array is named ARGV.

In the example above, our ARGC value is 4, while argv[] has five elements of char* type, namely "Hello.exe", "-M", "2", "1", and finally a null. ARGC is the number of arguments before null .

Then we can argv[i the contents of our command line parameters by means of the array subscript, and then we will implement our function. Usually, the ARGC is generally judged at the beginning of the program, whether it satisfies the number of parameters we want to access next, and if it is less than the minimum number of parameters we need, the program tends to collapse.

Another point to emphasize, or, these parameters are only formal parameter, the name is not fixed, you can change it, but we usually understand.

Obviously, every time we need to implement an operation, we need to go to the engineering-----> properties to change the command line parameters, this is not what we want, but the command line parameters are really important, which is mainly reflected in the CMD environment.

In the CMD environment to find the project we just built, debug found just the executable program (suffix. exe), after it followed our parameters, click Enter, the parameters will be obtained, will appear we want results.

To give a simple example, Fcopy.exe is an executable filethat implements file replication, and runs the fcopy format on the command line:

System prompt + Fcopy.exe In.dat out.dat

In.dat "and" out.dat "are command-line arguments, where both" In.dat "and" Out.dat "are available with a drive letter and a lookup path.

About Environment Variables

Simply put, "path" is a variable that stores the directory paths that are stored in common commands. Similar to the default DOS path, when the directory is added to the path, when running some programs in addition to looking in the current folder, but also to the default path set to find.

For example, path, is to tell the system, when the system is asked to run a program without telling it the full path of the program, in addition to the current directory under the search for this program, but also to what directories to look for.

In general, you can not see the meaning of the environment variables, but learned Java should know that after the Eclipse installation is to configure the environment variables, that is, the bin directory to replicate the past, specifically why, here is not much explanation. Another big problem that environment variables can solve is the software common problem of dual system. For example, in the C-drive and D-disk installation win7 and XP system, but some software is only installed under one system, the other system is often not used, the more troublesome way is to install it on another platform again.

Why is the software installed under one system not open in another system? The reason is that it is often necessary to copy some files to the system directory when installing the software, while using another system will not work because of the missing files. environment variables can be a good solution to this problem.

To view the environment variables, you can set the environment variables of the system through my Computer-property-〉 advanced, but do the environment variables set here have corresponding entries in the registry? The answer is yes.

650) this.width=650; "title=" 3.png "src=" Http://s2.51cto.com/wyfs02/M02/7F/67/wKiom1cdkYHx_uPHAAIBGcF3S1E698.png " alt= "Wkiom1cdkyhx_uphaaibgcf3s1e698.png"/>

About this aspect, at present is simply has had the understanding, wants in-depth understanding, here recommends two Web sites, for everybody to study

Define Environment variables:

Http://book.2cto.com/201309/33403.html

Environment variables and custom variables in the shell

http://edsionte.com/techblog/archives/3389

Main function--command-line arguments and environment variables

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.