Use of argc argv

Source: Internet
Author: User
Tags first string
 
During Turbo c2.0 startup, three parameters of the main () function are passed: argc, argv, and Env.
* Argc: the number of main () command line parameters.
* Argv: String Array.
Argv [0] indicates the full path name of the program.
Argv [1] is the first string after the program name is executed in the doscommand line;
Argv [2] is the second string after the execution program name;
...
Argv [argc] is null.
* Env: String Array. Each element of env [] contains characters in the form of envvar = value.
String. Envvar is the environment variable. Value is the corresponding value of envvar.
# Include <stdlib. h>
# Include <stdio. h> main (INT argc, char * argv [], char * env []) {
Int I;
Printf ("% d/N", argc);/* Why is its output 1? What is it defined? I don't understand the above explanation */
For (I = 0; I <= argc; I ++)
Printf ("argv [% d]: % s/n", I, argv [I]);
For (I = 0; ENV [I]! = NULL; I ++)
Printf ("env [% d]: % s/n", I, ENV [I]);} argc, argv, ENV are assigned values before the main () function, the executable file generated by the compiler. Main () is not a real entry point, but a standard function. The function name is related to the specific operating system.
Printf ("% d/N", argc) // output 1 is correct because the executable file name is the first parameter, that is, argv [0] = number of executable file names argc containing command line options, argv contains argc C-style strings, indicates the Command Options separated by spaces. For example, if you enter the name of the executable program, work.exe follows-d-o ....
Then argv [0] = work.exe argv [1] =-D argv [2] =-o
You can let them output it to an example:
# Include <stdio. h>;

Int main (INT argc, char * argv [])
{
Printf ("% d/N", argc );
While (argc)
Printf ("% s/n", argv [-- argc]);
Return 0;
}

Compile it as test.exe,
Under the command line,
Test hello
The output result is
2
Hello
Test

Main (INT argc, char * argv []), where argc refers to the number of variables. In this example, it refers to test and hello, that is, 2,
The argc must be at least 1. I don't need to say that.
Argv is an array of char *, which stores the content of the variable.
The example of storing test and argv [1] In argv [0] is really classic, so it is left behind. It is useful for understanding the argv [] function.

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.