Parameters of main function in C language

Keywords: C network Linux

Article directory


When the program is running, some need to take parameters, some do not take parameters, such as linux operating system commands, which are essentially C programs.

1) In Linux commands, there are not many without parameters.

pwd  #Show current directory
clear  #Clean screen

2) Most Linux commands have 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 C language learning. The C program written is very simple, and it does not need parameters to run. But in the actual development, the main function generally needs parameters, and there are very few cases without parameters.

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

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

1, Parameters of main function

The main function has three parameters, argc, argv and envp. Its standard writing method is as follows:

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

int argc, which stores the number of command line parameters.

char
*argv [] is an array of strings, each element is a character pointer, pointing to a string, that is, every parameter in the command line.

char
*envp [] is also an array of strings. Each element of this array is a character pointer to an environment variable.

Let's start with envp and talk about argc and argv.

Example (book101.c)

/*
 * Program name: book101.c, this program demonstrates the parameters of the main function.
 * Author: C language technology network (www.freecplus.net) date: 20190525
*/
#include <stdio.h>

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

  // Number of display parameters
  printf("argc is %d\n",argc);

  // List all parameters
  for (ii=0;ii<argc;ii++)
  {
    printf("argv[%d] is %s\n",ii,argv[ii]);
  }
}

Operation result

Note:

1) The value of argc is the number of parameters plus 1, because the program name is the first parameter of the program, namely argv[0]. In the above example, argv[0] is. / book101.

2) The parameters of the main function, whether they are written integers or floating-point numbers, are all considered strings.

3) argc and argv are programmer's conventions. You can also use argd or args, but this is not recommended.

2, Standard writing of C program

First, suppose that the program execution has parameters, that is, the main function has parameters, then how can the user know the number and meaning of the parameters of the program, and remember it? See user manual? No, good programmers will provide instructions in the program. Let's start with an example.

Example (book103.c)

/*
 * Program name: book103.c, this program demonstrates the parameters of the main function.
 * Author: C language technology network (www.freecplus.net) date: 20190525
*/
#include <stdio.h>

int main(int argc,char *argv[])
{
  if (argc!=6)
  {
    printf("\n This is a super girl draft program. Based on the super girl information provided, judge"\
           "Whether she meets the standards of the princess.\n\n");
    printf("Usage:./book103 name age height sc yz\n");
    printf("For example:./book103 Xishi 22 170 hot and beautiful\n");
    printf("name   Super girl's name.\n");
    printf("age    Super girl's age.\n");
    printf("height Supergirl height, unit: cm. \n");
    printf("sc     Super girl's body, hot; ordinary; airport.\n");
    printf("yz     Super girl's face is beautiful, average, and crooked melon split jujube.\n\n");

    return -1;
  }

  printf("The super girl information you entered is: name(%s),Age (%s),Height (%s),Figure (%s),Face value (%s). \n",\
          argv[1],argv[2],argv[3],argv[4],argv[5]);

  printf("Please wait while we calculate......\n");

  if (((atoi(argv[2]) >=  20) && (atoi(argv[2]) <=  30)) &&  // Age between 20-30
      ((atoi(argv[3]) >= 165) && (atoi(argv[3]) <= 175)) &&  // Height between 165-175
      ((strcmp(argv[4],"Hot and spicy")==0)                     ) &&  // Stature
      ((strcmp(argv[5],"Well done!")==0)                     ))    // Beautiful face
  {
    printf("Super girl (%s)Draft qualified, sent to the harem.\n",argv[1]); return 0;
  }
  else
  {
    printf("Super girl (%s)If the draft is not up to the standard, five Liang silver will be given and sent home.\n", argv[1]); return 0;
  }
}

When running the program, if the parameters are inconsistent, the running effect is as follows.

If the number of parameters matches, the operation effect is as follows.

It can be seen from the above examples that if the parameters provided during the execution of the program do not conform to the design, the instructions for the use of the program shall be displayed, and the explanatory text shall include the function of the program and the explanation of all parameters, as well as more detailed information such as the author and contact information.

The instructions for using the program are very important for several reasons:

1) Users of the program do not have to be able to write the program, nor need to check the user manual and other information;

2) Even if the user of the program can write the program, it is not necessary to look at the source code when using it, and you do not necessarily want him to see the source code;

3) If the user of the program is yourself, you will forget the parameters of the program after a long time.

Of course, these are only programmer's conventions, not C language's regulations.

3, envp parameter

envp stores the parameters of the current program running environment.

Example (book105.c)

/*
 * Program name: book105.c, which is used to demonstrate the parameter envp of the current program running environment.
 * Author: C language technology network (www.freecplus.net) date: 20190525
*/
#include <stdio.h>

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

  while (envp[ii] != 0)  // The last element of the array is 0
  {
    printf("%s\n",envp[ii]); ii++;
  }
}

Operation effect

Note that book105 runs the same as the env command on linux.

In the actual development, there are not many application scenarios of envp parameters, so you can understand them.

4, Homework after class

Write a sample program, and demonstrate all the knowledge points in this chapter. The program demonstration can deepen your understanding and image.

5, Copyright notice

C language technology net original article, reprint please explain the article source, the author and the original link.
Source: C language technology network (www.freecplus.net)
Author: Manon Youdao

If there is a mistake in the article, or there is a mistake in the content, or other suggestions and opinions, please leave a message for correction, thank you very much!!!

Published 20 original articles, won praise 1, visited 514
Private letter follow

Posted by comcentury on Tue, 03 Mar 2020 20:00:47 -0800