The main function of language parameters 20.C


When the program is running, some parameters need to bring some with no parameters, such as linux operating system commands, they are essentially that C programs.

1) Linux command, without much argument.

pwd  #显示当前目录
clear  #清屏

2) most of the Linux command parameters.

cp  book1.c book2.c
mkdir /tmp/dname
mv book3 /tmp/dname/book3
rm -rf /tmp/dname

You are now in the initial stage of learning C language, written in C program is very simple, run no arguments, but in the actual development, main function generally requires parameters, no parameters rare cases.

The main function parameters are passed when the execution of the program from a command prompt, such as ls command.

Here Insert Picture Description

In the example above ls command has two parameters, -l and book1? .C.

First, the main function of the parameters

There are three main function parameters, argc, argv and envp, its standard worded as follows:

int main(int argc,char *argv[],char *envp[])

int argc, storing the number of command line arguments.

char
* the argv [], is an array of strings, each element is a character pointer to a character string, i.e., a command parameter for each row.

char
* envp [], is a string array, each element of this array is a pointer to a character pointer to an environment variable.

envp first put it, to talk about the argc and argv.

Example (book101.c)

/*
 * 程序名:book101.c,此程序演示main函数的参数。
 * 作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>

int main(int argc,char *argv[])
{
  int ii=0;

  // 显示参数的个数
  printf("argc is %d\n",argc);

  // 列出全部的参数
  for (ii=0;ii<argc;ii++)
  {
    printf("argv[%d] is %s\n",ii,argv[ii]);
  }
}

operation result

Here Insert Picture Description

Note a few things:

1) argc value is the number of parameters plus one, because the first parameter is the program name of the program, i.e., argv [0], in the above example, argv [0] is ./book101.

Parameter 2) main function, either integer or floating-point writing, all are considered strings.

Named argc and argv 3) parameter is the programmer's convention, you can also use argd or args, but not recommended.

Second, the program specification written C

Assume that the program execution are all parameters, that is the main function has parameters, then the number of arguments and how users know the meaning of the program, remember it? See manual? No, good programmers will provide explanatory text in the program. Let's look at an example.

Example (book103.c)

/*
 * 程序名:book103.c,此程序演示main函数的参数。
 * 作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>

int main(int argc,char *argv[])
{
  if (argc!=6)
  {
    printf("\n这是一个超女选秀程序,根据提供的超女信息,判断"\
           "她是否符合王妃的标准。\n\n");
    printf("用法:./book103 name age height sc yz\n");
    printf("例如:./book103 西施 22 170 火辣 漂亮\n");
    printf("name   超女的姓名。\n");
    printf("age    超女的年龄。\n");
    printf("height 超女的身高,单位:cm。\n");
    printf("sc     超女的身材,火辣;普通;飞机场。\n");
    printf("yz     超女的颜值,漂亮;一般;歪瓜裂枣。\n\n");

    return -1;
  }

  printf("您输入的超女信息是:姓名(%s),年龄(%s),身高(%s),身材(%s),颜值(%s)。\n",\
          argv[1],argv[2],argv[3],argv[4],argv[5]);

  printf("正在计算中,请稍候......\n");

  if (((atoi(argv[2]) >=  20) && (atoi(argv[2]) <=  30)) &&  // 年龄在20-30之间
      ((atoi(argv[3]) >= 165) && (atoi(argv[3]) <= 175)) &&  // 身高在165-175之间
      ((strcmp(argv[4],"火辣")==0)                     ) &&  // 身材火辣
      ((strcmp(argv[5],"漂亮")==0)                     ))    // 颜值漂亮
  {
    printf("超女(%s)选秀合格,送往后宫。\n",argv[1]); return 0;
  }
  else
  {
    printf("超女(%s)选秀不合格,发放五两银子后送回家。\n", argv[1]); return 0;
  }
}

When running the program, if the parameter does not match, operating results are as follows.

Here Insert Picture Description

If the number matches the parameters, the following operation effect.

Here Insert Picture Description

As can be seen from the above example, if the time of execution of the program to provide design parameters and does not match, the program displayed instructions for use, descriptive text should include an explanation of all the parameters and functions of the program, but also including the author, contact information and more details information.

Using the program's text is very important for several reasons:

1) the user program will not necessarily write the program, there is no need to check the manual and other information;

User 2) Even if the program will write the program, I did not need to see the source code when in use, and you do not necessarily want him to see the source code;

3) If the user program is that you own, over time, you will forget the parameters of the program.

Of course, these are just programmers provisions of the agreement, not the C language.

Three, envp parameter

envp storing the parameters of the current program operating environment.

Example (book105.c)

/*
 * 程序名:book105.c,此程序用于演示当前程序运行环境的参数envp。
 * 作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>

int main(int argc,char *argv[],char *envp[])
{
  int ii = 0;

  while (envp[ii] != 0)  // 数组最后一个元素是0
  {
    printf("%s\n",envp[ii]); ii++;
  }
}

running result

Here Insert Picture Description

Attention, env book105 run of results with linux system command.

In the actual development, application scenarios envp parameter much, you look on the line.

Fourth, homework

Write the sample program, this section describes the knowledge of all the demo again, the demo program can deepen your understanding and mapping.

V. Copyright

C Language Technology Network original article, reproduced please indicate the source link to the article, the author and original.
Source: C Language Technology Network (www.freecplus.net)
Author: Ethics code Agriculture

If the article typos, or content errors, or other suggestions and comments, please correct me message, thank you very much! ! !

Published 29 original articles · won praise 2 · Views 675

Guess you like

Origin blog.csdn.net/m0_45133894/article/details/104649390