Lesson 33 main function and command line parameters

Posted May 25, 20201 min read

The concept of main function
· The main function in C language is called the main function
· A C program is executed from the main function
The main function is a function called by the operating system
· The main function is a function called by the operating system
The operating system always uses the main function as the beginning of the application
The operating system uses the return value of the main function as the exit status of the program
Example 33-2:

include "stdio.h"

int main()
{

printf("I \ m A! \ n");
return 99;
printf("I \ m b! \ n");

}
Output result:
Im A!
Main function parameters
· When the program is executed, you can pass parameters to the main function
int main()
int main(int argc)
int main(int argc, char \ * argc \ [])
int main(int argc, char _argc \ [], char_env \ [])
argc-Number of command line parameters
argc-array of command line arguments
env-array of environment variables
Example 33-3:

include "stdio.h"

int main(int argc, char argv \ [], char env \ [])
{

int i = 0;
printf("******** Begin argy ********* \ n");
for(i = 0; i <argc; i ++)
{
    printf("%s \ n", argv [i]);
}
printf("******** End argy ********* \ n");
printf("\ n");
printf("\ n");
printf("\ n");
printf("******** Begin env ********* \ n");
for(i = 0; env [i]! = NULL; i ++)
{
    printf("%s \ n", env [i]);
}
printf("******** End env ********* \ n");
return 0;

}
· Pay attention to the portability of functions
Summary:
· A C program is executed from the main function
· The main function is a function called by the operating system
· The main function has parameters and return values
Modern compilers support calling other functions before the main function