About the argv and envp parameters of the main function

Source: Internet
Author: User
Every C Program All must have a main () function, which can be placed somewhere in the program according to your hobbies. Some programmers put it at the beginning, while others put it at the end, no matter where it is, the following points are suitable.
1. Main () parameter
During Turbo c2.0 startup, three parameters of the main () function are passed: argc, argv, and Env.
* Argc: an integer that represents the number of command line parameters passed to main.
* Argv: String Array.
In dos 3.x, argv [0] is the full path name of the program;
For versions earlier than DOS 3.0, argv [0] is an empty string ("").
Argv [1] is the first string after the program name is executed in the DOS command line;
Argv [2] is the second string after the execution program name;
...
Argv [argc] is null. * Env: String Array. Each element of env [] contains a string in the form of envvar = value. Envvar is an environment variable, such as path or 87. Value is the corresponding value of envvar, such as c: \ dos, c: \ turboc (for Path) or yes (for 87 ).

When turboc2.0 is started, these three parameters are always passed to the main () function, which can be described (or not described) in the user program. If some (or all) parameters are specified, they become local variables of the main () subroutine. Note: Once you want to describe these parameters, they must be in the order of argc, argv, and env, as shown in the following example:

Main ()
Main (INT argc)
Main (INT argc, char * argv [])
Main (INT argc, char * argv [], char * env [])
The second case is also legal, but not common, because argc is rarely used in programs, rather than argv.
The following provides the example program example. EXE to demonstrate how to use three parameters in the main () function:

/* Program name example. EXE */
# Include
# Include
Main (INT argc, char * argv [], char * env [])
{
Int I;
Printf ("these are the % d command-line arguments passed to \ main: \ n", argc );
For (I = 0; I <= argc; I ++)
Printf ("argv [% d]: % s \ n", I, argv [I]);
Printf ("\ nthe environment string (s) on this system are: \ n ");
For (I = 0; ENV [I]! = NULL; I ++)
Printf ("env [% d]: % s \ n", I, ENV [I]);
}
Run the following command at the DOS prompt:

Example. EXE: C: \ example first_argument "argument with blanks" 3 4 "Last butone" Stop!

Note: Double quotation marks can be used to enclose parameters with spaces. For example, "argument with blanks" and "last but one ").

It should be noted that the maximum length of the command line parameter for transmitting the main () function is 128 characters (including spaces between parameters), which is restricted by DOS.

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.