Examples of three forms of the C language main function
- 2020-05-19 05:28:40
- OfStack
In the C language, the main() function has three forms.
1. No parameters
#include <stdio.h>
int main(void)
{
printf("Hello World!\n");
return 0;
}
2. There are two parameters
Traditionally, the first parameter is integer argc, which saves the number of parameters of the external calling command. The second parameter is pointer array or level 2 pointer argv, which saves the parameters corresponding to argc in the form of a string, as shown in the following example:
#include <stdio.h>
int main(int argc, char* argv[])
{
int i = 0;
for (; i < argc; i++) {
printf("%s\n", argv[i]);
}
printf("Hello World!\n");
return 0;
}
3. There are three parameters
An environment variable parameter is added on the basis of argc and argv. The form of the environment variable is "ENV=value", and the parameter type is pointer array or level 2 pointer, as shown in the following example:
int main(int argc, char* argv[], char* envp[])
{
int i = 0;
for (; envp[i] != '\0'; i++) {
printf("%s\n", envp[i]);
}
printf("Hello World!\n");
return 0;
}
Thank you for reading, I hope to help you, thank you for your support of this site!